I am looking for c# delegate version of this Manager using UnityEvent. I don\'t want to use this because UnityEvent is slower than C# event at most time.
Any clue on
!! Accepted answer is not complete !!
As a lazy programmer I simply copied what Programmer had written, but ran into the same problem people in the comment section ran into.
Programmer's solution does not work for multiple subscribers to the same event.
This is the fix (same changes for parameters version):
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class EventManager : MonoBehaviour
{
private Dictionary eventDictionary;
private static EventManager eventManager;
public static EventManager instance
{
get
{
if (!eventManager)
{
eventManager = FindObjectOfType(typeof(EventManager)) as EventManager;
if (!eventManager)
{
Debug.LogError("There needs to be one active EventManger script on a GameObject in your scene.");
}
else
{
eventManager.Init();
}
}
return eventManager;
}
}
void Init()
{
if (eventDictionary == null)
{
eventDictionary = new Dictionary();
}
}
public static void StartListening(string eventName, Action listener)
{
if (instance.eventDictionary.ContainsKey(eventName))
{
instance.eventDictionary[eventName] += listener;
}
else
{
instance.eventDictionary.Add(eventName, listener);
}
}
public static void StopListening(string eventName, Action listener)
{
if (instance.eventDictionary.ContainsKey(eventName))
{
instance.eventDictionary[eventName] -= listener;
}
}
public static void TriggerEvent(string eventName)
{
Action thisEvent = null;
if (instance.eventDictionary.TryGetValue(eventName, out thisEvent))
{
thisEvent.Invoke();
}
}
}
Here is a link to a StackOverflow question I posted on this
Why do I get a clone of Action<> when getting from dictionary?
When you call TryGetValue(eventName, out thisEvent) you are providing a reference to which the Dictionary will write the value. You are not getting a reference to what is inside the Dictionary (I mean, you are not getting a deep pointer to the Dictionary structure, meaning that assigning to it will NOT modify the Dictionary).