Skip to content

Instantly share code, notes, and snippets.

@nzhul
Created July 5, 2016 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nzhul/9904af49b5be03802f73daa18fda61ed to your computer and use it in GitHub Desktop.
Save nzhul/9904af49b5be03802f73daa18fda61ed to your computer and use it in GitHub Desktop.
Pseudo code for Unity3d EventManager. Basically this is implementation of Observer pattern but with some twists for Unity3d
using System;
using System.Collections.Generic;
namespace EventManager
{
public class EventManager
{
private static EventManager instance = null;
public static EventManager Instance
{
get { return instance; }
set { }
}
private Dictionary<EVENT_TYPE, List<IListener>> Listeners = new Dictionary<EVENT_TYPE, List<IListener>>();
void Awake()
{
if (instance == null)
{
instance = this;
// DontDestroyOnLoad(gameObject);
}
else
{
//DestroyImmediate(this);
}
}
public void AddListener(EVENT_TYPE eventType, IListener listener)
{
// List of listeners for this event
List<IListener> ListenList = null;
// Check existing event type key. If exists, add to
if (Listeners.TryGetValue(eventType, out ListenList))
{
// List exists, so add new item
ListenList.Add(listener);
return;
}
//Otherwise create new list as dictionary key
ListenList = new List<IListener>();
ListenList.Add(listener);
Listeners.Add(eventType, ListenList);
}
public void PostNotification(EVENT_TYPE eventType, object sender, object param = null)
{
// Notify all listeners of an event
// List of listeners for this event only
List<IListener> ListenList = null;
// If no event exists, then exit
if (!Listeners.TryGetValue(eventType, out ListenList))
{
return;
}
// Entry exists. Now notify appropriate listeners
for (int i = 0; i < ListenList.Count; i++)
{
if (!ListenList[i].Equals(null))
{
ListenList[i].OnEvent(eventType, sender, param);
}
}
}
public void RemoveEvent(EVENT_TYPE eventType)
{
Listeners.Remove(eventType);
}
// Remove all redundant entries from the Dictionary
public void RemoveRedundancies()
{
// Create new dictionary
Dictionary<EVENT_TYPE, List<IListener>> TempListeners = new Dictionary<EVENT_TYPE, List<IListener>>();
// Cycle through all dictionary entries
foreach (KeyValuePair<EVENT_TYPE, List<IListener>> item in Listeners)
{
// Cycle all listeners, remove null objects
for (int i = item.Value.Count - 1; i >= 0; i--)
{
// if null, then remove item
if (item.Value[i].Equals(null))
{
item.Value.RemoveAt(i);
}
}
// If items remain in list, then add to tmp dictionary
if (item.Value.Count > 0)
{
TempListeners.Add(item.Key, item.Value);
}
}
// Replace listeners object with new dictionary
Listeners = TempListeners;
}
// Called on scene change. Clean up dictionary
void OnLevelWasLoaded()
{
RemoveRedundancies();
}
}
// EVENT MANAGER END!!!!!!!
public enum EVENT_TYPE
{
INIT,
END,
AMMO_EMPTY,
HEALTH_CHANGE,
DEAD
}
public interface IListener
{
void OnEvent(EVENT_TYPE eventType, Object Sender, Object Param = null);
}
public class MyCustomListener : IListener
{
public void OnEvent(EVENT_TYPE eventType, Object Sender, object Param = null)
{
throw new NotImplementedException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment