Advertisement
nbannister

Unity Global Event Listener

Jan 30th, 2020
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.71 KB | None | 0 0
  1. //The commented lines can be restored if the project contains the Odin Inspector
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. //using Sirenix.OdinInspector;
  7.  
  8. //Add this component to a gameobject to listen for global events, and fire
  9. //an instance of unity event (set up in inspector) in response.
  10. public class GlobalEventListener : MonoBehaviour {
  11.     public string GlobalEventName;
  12.     public UnityEvent OnGlobalEventInvoked;
  13.  
  14.     //[DisableInEditorMode, Button, HorizontalGroup(Title = "Debug")]
  15.     public void DebugGlobalEventDictionary() {
  16.         GlobalEvent.DebugDictionary();
  17.     }
  18.  
  19.     //[DisableInEditorMode, Button, HorizontalGroup(Title = "Debug")]
  20.     public void InvokeGlobalEvent() {
  21.         GlobalEvent.Invoke(GlobalEventName);
  22.     }
  23.  
  24.     //[DisableInEditorMode, Button, HorizontalGroup(Title = "Debug")]
  25.     public void InvokeListenerEvent() {
  26.         OnGlobalEventInvoked.Invoke();
  27.     }
  28.  
  29.     private void OnEnable() {
  30.         GlobalEvent.Subscribe(GlobalEventName, InvokeListenerEvent);
  31.     }
  32.  
  33.     private void OnDisable() {
  34.         GlobalEvent.Unsubscribe(GlobalEventName, InvokeListenerEvent);
  35.     }
  36. }
  37.  
  38. //GlobalEvents can be fired from any code by calling GlobalEvent.Invoke(string)
  39. public static class GlobalEvent {
  40.     static Dictionary<string, Action> EventDictionary = new Dictionary<string, Action>();
  41.  
  42.     public static void DebugDictionary() {
  43.         foreach (string key in EventDictionary.Keys) {
  44.             int count = 0;
  45.             Action _action;
  46.             if (EventDictionary.TryGetValue(key, out _action)) {
  47.                 count = _action.GetInvocationList().Length;
  48.             }
  49.             Debug.Log("GlobalEvent " + key + ": " + count);
  50.         }
  51.     }
  52.  
  53.     public static void Subscribe(string name, Action action) {
  54.         Action _action;
  55.         if (EventDictionary.TryGetValue(name, out _action)) {
  56.             _action += action;
  57.             EventDictionary[name] = _action;
  58.         }
  59.         else {
  60.             EventDictionary.Add(name, action);
  61.         }
  62.     }
  63.  
  64.     public static void Unsubscribe(string name, Action action) {
  65.         Action _action;
  66.         if (EventDictionary.TryGetValue(name, out _action)) {
  67.             _action -= action;
  68.             if (_action == null) {
  69.                 EventDictionary.Remove(name);
  70.             }
  71.             else {
  72.                 EventDictionary[name] = _action;
  73.             }
  74.         }
  75.     }
  76.  
  77.     public static void Invoke(string name) {
  78.         Action _action;
  79.         if (EventDictionary.TryGetValue(name, out _action))
  80.             _action.Invoke();
  81.         else
  82.             Debug.LogWarning("No subscribers to GlobalEvent: " + name);
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement