C # Events Unity vs. net

Source: Internet
Author: User

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.

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.