So I’m using the EventManager code from this Unity tutorial,
I figured it would help clean up my beginner project. Although I would like to be able to pass a parameter with UnityAction.
In the tutorial, every function that is added as a listener is one without any parameters.
I was hoping I could find a way to do this without too much trouble, but I spent a while researching it, and it seems like the most popular answer is to use delegates/Action instead… I tried modifying it myself, and I feel like I was close, but I’m lost at the moment.
I’d rather avoid that extra work of rewriting what I’ve done, so here I am.
For an example of what I’d like to do, I was hoping that I could do something like this in my code, it’s invalid code, but it’s basically what I’m trying to achieve:
void Awake()
{
someListener = new UnityAction (SomeFunction); //<---Can't do this
}
void SomeFunction(GameObject someObject) //<--- Because this has a parameter
{
doSomething();
}
Below you can see what I mean, where the original examples are.
The EventTest.cs:
public class EventTest : MonoBehaviour
{
private UnityAction someListener;
void Awake ()
{
someListener = new UnityAction (SomeFunction); //<---This works,
}
void OnEnable ()
{
EventManager.StartListening ("test", someListener);
}
void SomeFunction () //<---Because no parameter here!
{
Debug.Log ("Some Function was called!");
}
}
And they call the TriggerEvent in another class, so it’s not directly referencing EventTest:
public class EventTriggerTest : MonoBehaviour {
void Update () {
if (Input.GetKeyDown ("q"))
{
EventManager.TriggerEvent ("test"); //No need to reference EventTest! Hooray!
}
}
And here is the EventManager.cs:
public class EventManager : MonoBehaviour {
private Dictionary <string, UnityEvent> eventDictionary;
private static EventManager eventManager;
public static EventManager instance
{
get
{
if (!eventManager)
{
eventManager = FindObjectOfType (typeof (EventManager)) as EventManager;
if (!eventManager)
Debug.LogError ("Need 1 active EventManger script on GameObject in your scene.");
else
eventManager.Init();
}
return eventManager;
}
}
void Init ()
{
if (eventDictionary == null)
eventDictionary = new Dictionary<string, UnityEvent>();
}
public static void StartListening (string eventName, UnityAction listener)
{
UnityEvent thisEvent = null;
if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
thisEvent.AddListener (listener);
else
{
thisEvent = new UnityEvent ();
thisEvent.AddListener (listener);
instance.eventDictionary.Add (eventName, thisEvent);
}
}
public static void StopListening (string eventName, UnityAction listener)
{
if (eventManager == null) return;
UnityEvent thisEvent = null;
if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
thisEvent.RemoveListener (listener);
}
public static void TriggerEvent (string eventName)
{
UnityEvent thisEvent = null;
if (instance.eventDictionary.TryGetValue (eventName, out thisEvent))
thisEvent.Invoke ();
}
}