SHARE
TWEET

Unity EventManager & EventSubscriber

a guest Jan 21st, 2020 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2.  
  3. //wrapper for subscribing to the EventManager
  4. public class EventSubscriber
  5. {
  6.     string tag;
  7.  
  8.     EventManager.GameFunction function;
  9.  
  10.  
  11.     /// <summary>
  12.     ///
  13.     /// </summary>
  14.     /// <param name="givenTag"> tag given to the EventManager. Symbolizes and identifies which event is being called</param>
  15.     /// <param name="givenFunction"> function executed when an event is called with the same tag </param>
  16.     public EventSubscriber(string givenTag, EventManager.GameFunction givenFunction)
  17.     {
  18.         tag = givenTag;
  19.         function = givenFunction;
  20.         EventManager.SubscribeToEvent(tag, function);
  21.     }
  22.  
  23.     /// <summary>
  24.     /// MUST be called when the GameObject holding an EventSubscriber is destroyed
  25.     /// Unsubscribes from the event
  26.     /// </summary>
  27.     public void Destroy()
  28.     {
  29.         EventManager.UnsubscribeToEvent(tag, function);
  30.         function = null;
  31.     }
  32. }
  33.  
  34. public class EventManager
  35. {
  36.     //callbacks needn't return anything to their caller, take an unspecified object
  37.     //note this object's type should be checked at runtime.
  38.     public delegate void GameFunction(object obj);
  39.  
  40.     //the event manager has a dictionary which binds an event type enum with a delegate
  41.     private static Dictionary<string, GameFunction> subscriptionList = new Dictionary<string, GameFunction>();
  42.  
  43.     /// <summary>
  44.     /// Calls all functions which have subscribed to givenEventType
  45.     /// </summary>
  46.     /// <param name="givenTag">string symbolizing and identifying which event is being called</param>
  47.     /// <param name="obj">Defaults to null. The parameter which will be given to the called functions.</param>
  48.     public static void BroadcastEvent(string givenTag, object obj = null)
  49.     {
  50.         if (subscriptionList.TryGetValue(givenTag, out GameFunction subscribers))
  51.         {
  52.             subscribers?.Invoke(obj);
  53.         }
  54.     }
  55.  
  56.     ///<summary>
  57.     /// Adds givenFunction to the delegate called on givenEventType
  58.     /// - you MUST use UnsubscribeToEvent on destroy of your object, otherwise you WILL face memory leaks
  59.     ///</summary>
  60.     ///<param name="givenTag"> Tag on which the function will be called.</param>
  61.     ///<param name="givenFunction"> Function taking an object as parameter which will be called on BroadcastEvent.</param>
  62.     public static void SubscribeToEvent(string givenTag, GameFunction givenFunction)
  63.     {
  64.         if (!subscriptionList.ContainsKey(givenTag))
  65.         {
  66.             subscriptionList[givenTag] = givenFunction;
  67.         }
  68.         else
  69.         {
  70.             subscriptionList[givenTag] += givenFunction;
  71.         }
  72.     }
  73.  
  74.     ///<summary>
  75.     ///if an object doesn't unsubscribe from an event on destroy, the event manager will still hold a pointer to said object, meaning said object will not be garbage collected
  76.     ///</summary>
  77.     ///<param name="givenEventType"> Tag on which the function was be called.</param>
  78.     ///<param name="givenFunction"> Function which the calling object was subscribing with.</param>
  79.     public static void UnsubscribeToEvent(string givenEventType, GameFunction givenFunction)
  80.     {
  81.         if (subscriptionList.ContainsKey(givenEventType))
  82.         {
  83.             subscriptionList[givenEventType] -= givenFunction;
  84.         }
  85.     }
  86. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top