I’ve read through documentation on events and watched a couple tutorials, but there is something I’m still not grasping. In the examples I’ve seen of Event Managers, the method that fires the event is in the same class as the Event Manager. 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, the EventManager not only defines the delegate and event, but also creates a button which fires the event.
This is what confuses me. Somehow I thought the EventManager would do nothing but define the events. Then you would fire those events from other scripts. Is there a way to do that?
I don’t like the way this example is set up because it seems that I would have to attach an “EventManager” script to every object that could possibly fire an event. What if there are multiple objects that can fire the same event? Do I need to attach the same “EventManager” to every one?
You can achieve this without dragging and dropping the EventManager instance into each script that needs it. You can check this post: (what you’re looking for would be at the bottom): Quick Tip: An Efficient Event Delegation Approach in Unity
I think I may have answered my own question, but maybe someone can tell me if this will perform well or not.
I set up the event triggers in EvenManager as public methods, and then I used the drag-drop method to place the EventManager in other classes, making its methods accessible to anything that wants to fire an event. So my setup looks like this (all of these are attached to separate game objects):
using UnityEngine;
using System.Collections;
public class EventManager : MonoBehaviour {
//Here I define the delegates, events and the triggers that can be 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 class EventTrigger : MonoBehaviour {
/******
In this class I can set up a method that will access the EventManager
(which has been dropped onto 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 class EventListener : 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);
}
}