Use unityevent prior to unity4.6

Source: Internet
Author: User

??

Sun Guangdong: 2015-3-8/18:43 reprint Please specify the Source: http://blog.csdn.net/u010019717

More full content Please see my game Bull address: http://www.unitymanual.com/space-uid-18602.html


After unity4.6 a little more of this stuff.

Of course it should be used in the UI. So is there any other use?

Let's take a look at the official bull 's introduction to Unityevents:
Unityevents is a way to allow users to drive callbacks from the time of editing, without additional programming and scripting configuration.
Unityevents is useful in a number of things:
? Content-driven callback contents driven callbacks
Decoupling system decoupling systems
? Persistent callback Persistent callbacks
Pre-configured Call events preconfigured

Unityevents can be added to any of the Monobehaviour classes and is executed.

How does he know about the delegate delegate like the. NET Standard? 1, look at the source code ah, 2, as search:

Click to enter:


When Unityevent is added to Monobehaviour, it appears in the inspector and you can add persistent callbacks.

Using unityevents
There are several steps to configuring a callback in the editor:
1. Make sure that your script imports/uses the using Unityengine.events.
2. Select the + icon to add a slot for the callback method
3. Select Unityengine.object you wish to receive (you can use the selector for this object )
4. Select the function you want to call
5. You can add more callbacks about an event

There are two types of supported function calls when configuring Unityevent in the Inspector Inspector:
? Static statics. A static call is a pre-configured call that is set in the UI of a preconfigured value. This means that when the callback is invoked, the target function is invoked using parameters that have been entered into the UI.
? Dynamic Dynamics. Use code to invoke the dynamic call from the parameters that are sent, which binds to the type that the unityevent is calling. The UI filter callback and displays only valid unityevent dynamic calls.

Generic unityevents
By default, the unityevent in Monobehaviour is bound to the void function dynamically. This does not have to be the case for unityevents dynamic calls that support binding to functions with up to 4 parameters. To do this, you need to define a custom unityevent class that supports multiple parameters.

It is very easy to do:

[Serializable]public class Stringevent:unityevent <string> {}


This instance is added to your class instead of base Unityevent, which allows callbacks to be dynamically bound to string functions. This can then be called using a string as a parameter by calling the Invoke () function.

Unityevents can be defined in its generic definition with up to 4 parameters.

Example:


1, Unityengine.events Classes
Unityeventbase class:
Namespace:UnityEngine.Events
Description: Abstract base class for unityevents.
This class provides the basic functionality for unityevents.
Unityevent class:
Namespace:unityengine.events/inherits From:Events.UnityEventBase
Description: The 0 parameter continuous callback can be saved on-site.
Example:

Using unityengine;using system.collections;using Unityengine.events;public class Event0:monobehaviour {public Unityevent m_myevent;    Note that this must be public in order to be applied on Inspector by +-number operation//use of this for Initializationvoid Start () {if (m_myevent = = null) M_myevent = new Unityevent (); M_myevent.addlistener (Ping);} Update is called once per framevoid Update () {if (Input.anykeydown && m_myevent! = null) {M_myevent.invoke ();} }public void Ping () {Debug.Log ("Ping");}}



Unityevent<t0> class:
Namespace:unityengine.events/inherits From:Events.UnityEventBase
Description: A unityevent version of a parameter.
If you want to use the generic type of unityevent, you must override the class type.
Example:

Using unityengine;using system.collections;using Unityengine.events;public class Myintevent:unityevent<int> {} public class Event1:monobehaviour {public myintevent m_myevent;//use this for initializationvoid Start () {if (M_myeven T = = null) M_myevent = new Myintevent (); M_myevent.addlistener (Ping);} Update is called once per framevoid Update () {if (Input.anykeydown && m_myevent! = null) {M_myevent.invoke (5); }}void Ping (int i) {Debug.Log ("ping" + i);}}




Unityevent<t0,t1> class:
Namespace:unityengine.events/inherits From:Events.UnityEventBase
Description: Unityevent version of two parameters.
If you want to use the generic type of unityevent, you must override the class type.
Example:

Using unityengine;using system.collections;using unityengine.events;public class my2argevent:unityevent<int, int > {}public class Event2:monobehaviour {public my2argevent m_myevent;//with this for initializationvoid Start () {if ( M_myevent = = null) M_myevent = new My2argevent (); M_myevent.addlistener (Ping);} Update is called once per framevoid Update () {if (Input.anykeydown && m_myevent! = null) {M_myevent.invoke (5, 6);}} void Ping (int i0, int i1) {Debug.Log (I0 + I1);}}



Unityevent<t0,t1,t2> class:
Namespace:unityengine.events/inherits From:Events.UnityEventBase
Description: Unityevent version of 3 parameters.
If you want to use the generic type of unityevent, you must override the class type.
Example:

Using unityengine;using system.collections;using unityengine.events;public class my3argevent:unityevent<int, int, int> {}public class Event3:monobehaviour {public my3argevent m_myevent;//use this for initializationvoid Start () {i F (m_myevent = = null) M_myevent = new My3argevent (); M_myevent.addlistener (Ping);} Update is called once per framevoid Update () {if (Input.anykeydown && m_myevent! = null) {M_myevent.invoke (5, 6, 7);}} void Ping (int i0,int i1,int i2) {Debug.Log (I0 + i1 + i2);}}


unityevent<t0,t1,t2,t3> class:
Namespace:unityengine.events/inherits From:Events.UnityEventBase
Description: Unityevent version of 4 parameters.
If you want to use the generic type of unityevent, you must override the class type.
Example:

Using system.collections;using unityengine.events;public class my4argevent:unityevent<int, int, int, int> {} public class Event4:monobehaviour {public my4argevent m_myevent;//use this for initializationvoid Start () {if (M_myeve NT = = null) M_myevent = new My4argevent (); M_myevent.addlistener (Ping);} Update is called once per framevoid Update () {if (Input.anykeydown && m_myevent! = null) {M_myevent.invoke (5, 6, 7,-5);}} void Ping (int i0,int i1,int i2,int i3) {Debug.Log (I0 + i1 + i2 + i3);}}


2 Uni tyengine.events Enumerations
Persistentlistenermode
Description: The operating mode of the listener.
Unityeventcallstate
Description: Controls the scope of the unityevent callback.


Know its usage, then go back to the headline, basically now the project is still using Ngui so will not use the unity4.6 version.

But don't want to miss the good things that unity was born with. What to do? Then get the source code.

I have no problem after the test!

as follows: http://download.csdn.net/detail/u010019717/8483193

Use unityevent prior to unity4.6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.