Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- public class EventManager {
- private static Dictionary<Events, List<IEventListener>> listeners = new Dictionary<Events, List<IEventListener>>();
- // add listener to specified event
- public static void AddListener(IEventListener obj, Events eventKey)
- {
- if (!listeners.ContainsKey(eventKey))
- {
- listeners.Add(eventKey, new List<IEventListener>());
- }
- listeners[eventKey].Add(obj);
- }
- // remove listener from specified event
- public static void RemoveListener(IEventListener obj, Events eventKey)
- {
- List<IEventListener> listenerObjects = listeners[eventKey];
- if (listeners != null)
- {
- if (listenerObjects.Contains(obj))
- {
- listenerObjects.Remove(obj);
- }
- }
- }
- // remove listener from all events
- public static void RemoveListener(IEventListener obj)
- {
- foreach (KeyValuePair<Events, List<IEventListener>> entry in listeners)
- {
- RemoveListener(obj, entry.Key);
- }
- }
- // send event (with no data obj)
- public static void Send(Events eventKey)
- {
- Send(eventKey, null);
- }
- // send event
- public static void Send(Events eventKey, object data)
- {
- foreach (IEventListener obj in listeners[eventKey])
- {
- obj.Event(eventKey, data);
- }
- }
- }
- public interface IEventListener {
- void Event(Events eventKey, object data);
- }
Add Comment
Please, Sign In to add comment