Unity EventManager con delegato invece di UnityEvent

Sto cercando la versione del delegato c # di questo Manager usando UnityEvent . Non voglio usarlo perché UnityEvent è più lento dell’evento C # nella maggior parte dei casi.

Qualche idea su come implementare questo?

Puoi usare Action che in realtà è un delegato dichiarato come questo:

 namespace System { public delegate void Action(); } 

1. Sostituisci tutte le UnityAction con Action dallo spazio System nomi System che usa i delegati.

2. Sostituisci tutto thisEvent.AddListener(listener); con thisEvent += listener;

3. Sostituisci tutto thisEvent.RemoveListener(listener); con thisEvent -= listener;

Ecco la versione modificata di EventManager originale di EventManager portato per utilizzare delegato / Azione.

Senza parametro:

 using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class EventManager : MonoBehaviour { private Dictionary eventDictionary; private static EventManager eventManager; public static EventManager instance { get { if (!eventManager) { eventManager = FindObjectOfType(typeof(EventManager)) as EventManager; 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(); } } public static void StartListening(string eventName, Action listener) { Action thisEvent; if (instance.eventDictionary.TryGetValue(eventName, out thisEvent)) { //Add more event to the existing one thisEvent += listener; //Update the Dictionary instance.eventDictionary[eventName] = thisEvent; } else { //Add event to the Dictionary for the first time thisEvent += listener; instance.eventDictionary.Add(eventName, thisEvent); } } public static void StopListening(string eventName, Action listener) { if (eventManager == null) return; Action thisEvent; if (instance.eventDictionary.TryGetValue(eventName, out thisEvent)) { //Remove event from the existing one thisEvent -= listener; //Update the Dictionary instance.eventDictionary[eventName] = thisEvent; } } public static void TriggerEvent(string eventName) { Action thisEvent = null; if (instance.eventDictionary.TryGetValue(eventName, out thisEvent)) { thisEvent.Invoke(); // OR USE instance.eventDictionary[eventName](); } } } 

Script di prova:

Lo script di test sotto verifica l’evento triggersndo eventi ogni 2 secondi.

 public class TestScript: MonoBehaviour { private Action someListener; void Awake() { someListener = new Action(SomeFunction); StartCoroutine(invokeTest()); } IEnumerator invokeTest() { WaitForSeconds waitTime = new WaitForSeconds(2); while (true) { yield return waitTime; EventManager.TriggerEvent("test"); yield return waitTime; EventManager.TriggerEvent("Spawn"); yield return waitTime; EventManager.TriggerEvent("Destroy"); } } void OnEnable() { EventManager.StartListening("test", someListener); EventManager.StartListening("Spawn", SomeOtherFunction); EventManager.StartListening("Destroy", SomeThirdFunction); } void OnDisable() { EventManager.StopListening("test", someListener); EventManager.StopListening("Spawn", SomeOtherFunction); EventManager.StopListening("Destroy", SomeThirdFunction); } void SomeFunction() { Debug.Log("Some Function was called!"); } void SomeOtherFunction() { Debug.Log("Some Other Function was called!"); } void SomeThirdFunction() { Debug.Log("Some Third Function was called!"); } } 

Con parametro:

Da altre domande, la maggior parte delle persone chiede come supportare i parametri. Ecco qui. Puoi usare class / struct come parametro quindi aggiungere tutte le variabili che vuoi passare alla funzione all’interno di questa class / struct. EventParam come esempio. Sentiti libero di aggiungere / rimuovere le variabili che vuoi passare nella struttura EventParam dell’evento alla fine di questo codice.

 using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class EventManager : MonoBehaviour { private Dictionary> eventDictionary; private static EventManager eventManager; public static EventManager instance { get { if (!eventManager) { eventManager = FindObjectOfType(typeof(EventManager)) as EventManager; 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>(); } } public static void StartListening(string eventName, Action listener) { Action thisEvent; if (instance.eventDictionary.TryGetValue(eventName, out thisEvent)) { //Add more event to the existing one thisEvent += listener; //Update the Dictionary instance.eventDictionary[eventName] = thisEvent; } else { //Add event to the Dictionary for the first time thisEvent += listener; instance.eventDictionary.Add(eventName, thisEvent); } } public static void StopListening(string eventName, Action listener) { if (eventManager == null) return; Action thisEvent; if (instance.eventDictionary.TryGetValue(eventName, out thisEvent)) { //Remove event from the existing one thisEvent -= listener; //Update the Dictionary instance.eventDictionary[eventName] = thisEvent; } } public static void TriggerEvent(string eventName, EventParam eventParam) { Action thisEvent = null; if (instance.eventDictionary.TryGetValue(eventName, out thisEvent)) { thisEvent.Invoke(eventParam); // OR USE instance.eventDictionary[eventName](eventParam); } } } //Re-usable structure/ Can be a class to. Add all parameters you need inside it public struct EventParam { public string param1; public int param2; public float param3; public bool param4; } 

Script di prova:

 public class Test : MonoBehaviour { private Action someListener1; private Action someListener2; private Action someListener3; void Awake() { someListener1 = new Action(SomeFunction); someListener2 = new Action(SomeOtherFunction); someListener3 = new Action(SomeThirdFunction); StartCoroutine(invokeTest()); } IEnumerator invokeTest() { WaitForSeconds waitTime = new WaitForSeconds(0.5f); //Create parameter to pass to the event EventParam eventParam = new EventParam(); eventParam.param1 = "Hello"; eventParam.param2 = 99; eventParam.param3 = 43.4f; eventParam.param4 = true; while (true) { yield return waitTime; EventManager.TriggerEvent("test", eventParam); yield return waitTime; EventManager.TriggerEvent("Spawn", eventParam); yield return waitTime; EventManager.TriggerEvent("Destroy", eventParam); } } void OnEnable() { //Register With Action variable EventManager.StartListening("test", someListener1); EventManager.StartListening("Spawn", someListener2); EventManager.StartListening("Destroy", someListener3); //OR Register Directly to function EventManager.StartListening("test", SomeFunction); EventManager.StartListening("Spawn", SomeOtherFunction); EventManager.StartListening("Destroy", SomeThirdFunction); } void OnDisable() { //Un-Register With Action variable EventManager.StopListening("test", someListener1); EventManager.StopListening("Spawn", someListener2); EventManager.StopListening("Destroy", someListener3); //OR Un-Register Directly to function EventManager.StopListening("test", SomeFunction); EventManager.StopListening("Spawn", SomeOtherFunction); EventManager.StopListening("Destroy", SomeThirdFunction); } void SomeFunction(EventParam eventParam) { Debug.Log("Some Function was called!"); } void SomeOtherFunction(EventParam eventParam) { Debug.Log("Some Other Function was called!"); } void SomeThirdFunction(EventParam eventParam) { Debug.Log("Some Third Function was called!"); } }