Delegate & event

Source: Internet
Author: User

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

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.