Skip to content

Instantly share code, notes, and snippets.

@wmiller
Last active October 22, 2023 06:39
Show Gist options
  • Save wmiller/3903205 to your computer and use it in GitHub Desktop.
Save wmiller/3903205 to your computer and use it in GitHub Desktop.
Unity3D Event System
using System.Collections;
using System.Collections.Generic;
public class GameEvent
{
}
public class Events
{
static Events instanceInternal = null;
public static Events instance
{
get
{
if (instanceInternal == null)
{
instanceInternal = new Events();
}
return instanceInternal;
}
}
public delegate void EventDelegate<T> (T e) where T : GameEvent;
private delegate void EventDelegate (GameEvent e);
private Dictionary<System.Type, EventDelegate> delegates = new Dictionary<System.Type, EventDelegate>();
private Dictionary<System.Delegate, EventDelegate> delegateLookup = new Dictionary<System.Delegate, EventDelegate>();
public void AddListener<T> (EventDelegate<T> del) where T : GameEvent
{
// Early-out if we've already registered this delegate
if (delegateLookup.ContainsKey(del))
return;
// Create a new non-generic delegate which calls our generic one.
// This is the delegate we actually invoke.
EventDelegate internalDelegate = (e) => del((T)e);
delegateLookup[del] = internalDelegate;
EventDelegate tempDel;
if (delegates.TryGetValue(typeof(T), out tempDel))
{
delegates[typeof(T)] = tempDel += internalDelegate;
}
else
{
delegates[typeof(T)] = internalDelegate;
}
}
public void RemoveListener<T> (EventDelegate<T> del) where T : GameEvent
{
EventDelegate internalDelegate;
if (delegateLookup.TryGetValue(del, out internalDelegate))
{
EventDelegate tempDel;
if (delegates.TryGetValue(typeof(T), out tempDel))
{
tempDel -= internalDelegate;
if (tempDel == null)
{
delegates.Remove(typeof(T));
}
else
{
delegates[typeof(T)] = tempDel;
}
}
delegateLookup.Remove(del);
}
}
public void Raise (GameEvent e)
{
EventDelegate del;
if (delegates.TryGetValue(e.GetType(), out del))
{
del.Invoke(e);
}
}
}
@mikelovesrobots
Copy link

Mind assigning a license to this so I can use it my own projects?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment