Skip to content

Instantly share code, notes, and snippets.

@wmiller wmiller/Events.cs
Last active Oct 17, 2019

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

mikelovesrobots commented Dec 17, 2013

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
You can’t perform that action at this time.