??
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