Event Manager and Event Listener

Source: Internet
Author: User

??

I have read the file about the event and watched a couple of tutorials, but there are still some I don't hold. in the Event manager example that I have seen, the event is triggered by the same class as the event manager managers. like this:

Using Unityengine; Using System.Collections;  public class Eventmanager:monobehaviour {public          delegate void Checkpointhandler (int id);     public static event Checkpointhandler checkpointreached;      void Ongui () {              if (GUI. button (new Rect (5,5,150,40), "Checkpoint")) {             checkpointreached (555);}}  }

So here, EventManager not only defines delegates and events, but also creates a button that will trigger the event.

This is what makes me confused. thinking that EventManager will do nothing is just a defined event. then you'll fire those events from other scripts. is there any way to do that?

I don't like this example setting because it seems that the "EventManager" script must be attached to each object that might trigger the event. can I raise the same event if there are multiple objects? do I need to attach the same "EventManager" to everyone?

I think I might answer my own questions, but maybe someone can tell me whether this will perform successfully or not.

I set up an event trigger in Evenmanager, as a public method, and then use the drag-and-drop method to place EventManager in another class so that its methods can be any event that you want to trigger. So my settings are as follows (all of which are connected to a standalone game object):

Using Unityengine;  Using System.Collections; public classEventManager: monobehaviour {//here I define the delegates, events and the triggers that can is called to fire the event.     public delegate void Checkpointhandler (int id);           public static event Checkpointhandler checkpointreached;     public void hitcheckpoint (int id) {if (checkpointreached! = NULL) checkpointreached (ID); }} using Unityengine;  Using System.Collections; public classEventTrigger: monobehaviour {/****** In this class I can set up a method that would access the EventManager (which has been dropped O Nto the EM variable) and fire the Hitcheckpoint method, which in turn sends the Checkpointreached event.      /Public EventManager em; void Ongui () {if (GUI.         button (new Rect (5,5,150,40), "Checkpoint")) {em.hitcheckpoint (555); }}} using Unityengine;  Using System.Collections; public classEventListener: monobehaviour {//this script listens for the event and prints a message to the log.     void Onenable () {eventmanager.checkpointreached + = handleeventmanagercheckpointreached;     } void Ondisable () {eventmanager.checkpointreached-= handleeventmanagercheckpointreached; } void handleeventmanagercheckpointreached (int id) {Debug.Log ("Checkpoint reahed.     ID is: "+ ID); } }








Event Manager and Event Listener

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.