Event:(Events in unity3d are used to replace the sendmessage mechanism)
C # event definition,
1: first, a delegate is required, which is equivalent to the Event Response. C # comes with an eventhandler that accepts the sender object and the event parameter eventargs (the event parameter type that comes with C ).
You can also write your own delegate and pass the parameters you want, without using the C # built-in eventhandler and eventargs.
1Public Delegate VoidMyeventhandler (event EVT );
2: Define an event and bind the event to your defined delegate.
1 Public EventMyeventhandlerMyevthandlers;
3: Then you need to add a method (trigger) to trigger this event.
1Public VoidDispatchevent (event EVT)2 {3 Myevthandlers(EVT );4}
4: the definition of such an event is complete. This is only used for calling. You can use +-to add or delete event responses.
1 Class Mainclass 2 { 3 Public Static Void Main ( String [] ARGs) 4 { 5 Console. writeline ( " Hello event! " ); 6 7 Eventdispatcher ED = New Eventdispatcher ();// The first three steps are defined in the eventdispatcher class. The registration event method is implemented in different classes. 8 Ed. myevthandlers + = New Myeventhandler (onevt ); 10 Event EVT = New Event ( " Fuckname " , True , Null ); 11 Ed. dispatchevent (EVT ); 12 } 13 14 Private Static Void Onevt (event EVT) 15 { 16 Console. writeline ( " Event name {0} " , EVT. evtname ); 17 } 18 }
Refer:
1. http://www.everyday3d.com/blog/index.php/2010/10/04/c-events-and-unity3d/
2. http://msdn.microsoft.com/en-us/library/aa645739%28VS.71%29.aspx
3, http://forum.unity3d.com/threads/38094-Is-SendMessage-really-that-bad? P = 245970 # post245970