Today, looking at Unity3d's book, The above call event is more special, such as button events
This method is written in the Ongui method.
If(Guilayout.button (" button 1")) { // Execute event processing }
This looks like Unityengine help us to change the incident in the Ongui event let us listen to ourselves to monitor our own created control, think and think again,
Why do this, so I wrote an alarm clock in the console, simulating the next alarm bell event, the code is as follows:
usingMicrosoft.VisualStudio.DebuggerVisualizers;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceunittest{/// <summary> ///event Argument class/// </summary> Public classRingingeventargs:eventargs { PublicRingingeventargs (datetime datetime) { time=DateTime; } Public ReadOnlyDateTime time; } /// <summary> ///Alarm Class/// </summary> Public classAlarm {//List of events set PrivateList<datetime> list=NewList<datetime>(); //increase the alarm's constructor function Public voidAdd (datetime datetime) { This. List.add (DateTime); } //Delegate Public Delegate voidAlarmhandler (Objectsender, Ringingeventargs e); //Events Public EventAlarmhandler ringing; /// <summary> ///methods for invoking events/// </summary> /// <param name= "EventArgs" ></param> protected Virtual voidonringing (Ringingeventargs eventArgs) {if(ringing!=NULL) {Ringing ( This, EventArgs); } } /// <summary> ///Alarm Clock Monitoring method/// </summary> Public voidCheckalarm () { while(true) { foreach(DateTime datetimeinchList) { if(DateTime.ToString ("YYYY-MM-DD hh:mm") = = DateTime.Now.ToString ("YYYY-MM-DD hh:mm") {onringing (NewRingingeventargs (DateTime.Now)); } } } } }}
Invoke the Detect alarm method to trigger an event
Static voidMain (string[] args) {Alarm Alarm=NewAlarm (); Alarm. ADD (DateTime.Parse ("2014-8-8 16:28")); Alarm. Ringing+=alarm_ringing; Alarm. Checkalarm (); } Private Static voidAlarm_ringing (Objectsender, Ringingeventargs e) {Console.WriteLine ("Enable alarm"+e.time.toshorttimestring ()); Console.ReadLine (); }
The event from C # is supposed to be a delegate, and it's not customary in unityengine.