Last active
December 18, 2021 23:34
-
-
Save sam-keene/3b183d9b7063a4510fb968620437365b to your computer and use it in GitHub Desktop.
Unity EventManager using UnityEvents and a HashTable to pass data of any type as a parameter in the event.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine.Events; | |
using System.Collections; | |
using System.Collections.Generic; | |
/* | |
Unity C# Event manager using UnityEvents and a Hashtable for loosely typed params with event. | |
This EventManager expands the usefulness of UnityEvents by allowing values of any type to be passed as a | |
parameter in the Event eg: int, string, Vector3 etc. | |
Usage: | |
// Add Listener for Event | |
SDKBoyEventManager.StartListening ("MY_EVENT", MyEventHandlerMethodName); | |
// Trigger Event: | |
SDKBoyEventManager.TriggerEvent ("MY_EVENT", new Hashtable(){{"MY_EVENT_KEY", "valueOfAnyType"}}); | |
// Pass null instead of a Hashtable if no params | |
SDKBoyEventManager.TriggerEvent ("MY_EVENT", null); | |
// Handler | |
private void HandleTeleportEvent (Hashtable eventParams){ | |
if (eventParams.ContainsKey("MY_EVENT")){ | |
// DO SOMETHING | |
} | |
} | |
*/ | |
public class SDKBoyEvent : UnityEvent <Hashtable> {} | |
public class SDKBoyEventManager : MonoBehaviour { | |
private Dictionary <string, SDKBoyEvent> eventDictionary; | |
private static SDKBoyEventManager eventManager; | |
// SINGLETON | |
public static SDKBoyEventManager instance | |
{ | |
get | |
{ | |
if (!eventManager) | |
{ | |
eventManager = FindObjectOfType (typeof (SDKBoyEventManager)) as SDKBoyEventManager; | |
if (!eventManager) | |
{ | |
Debug.LogError ("There needs to be one active EventManger script on a GameObject in your scene."); | |
} | |
else | |
{ | |
eventManager.Init (); | |
} | |
} | |
return eventManager; | |
} | |
} | |
void Init () | |
{ | |
if (eventDictionary == null) | |
{ | |
eventDictionary = new Dictionary<string, SDKBoyEvent>(); | |
} | |
} | |
public static void StartListening (string eventName, UnityAction<Hashtable> listener) | |
{ | |
SDKBoyEvent thisEvent = null; | |
if (instance.eventDictionary.TryGetValue (eventName, out thisEvent)) | |
{ | |
thisEvent.AddListener (listener); | |
} | |
else | |
{ | |
thisEvent = new SDKBoyEvent (); | |
thisEvent.AddListener (listener); | |
instance.eventDictionary.Add (eventName, thisEvent); | |
} | |
} | |
public static void StopListening (string eventName, UnityAction<Hashtable> listener) | |
{ | |
if (eventManager == null) return; | |
SDKBoyEvent thisEvent = null; | |
if (instance.eventDictionary.TryGetValue (eventName, out thisEvent)) | |
{ | |
thisEvent.RemoveListener (listener); | |
} | |
} | |
public static void TriggerEvent (string eventName, Hashtable eventParams = default(Hashtable)) | |
{ | |
SDKBoyEvent thisEvent = null; | |
if (instance.eventDictionary.TryGetValue (eventName, out thisEvent)) | |
{ | |
thisEvent.Invoke (eventParams); | |
} | |
} | |
public static void TriggerEvent (string eventName) | |
{ | |
TriggerEvent (eventName, null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment