栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

带有委托而不是UnityEvent的Unity EventManager

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带有委托而不是UnityEvent的Unity EventManager

您可以使用

Action
实际上是这样声明的委托:

namespace System{    public delegate void Action();}

1。 从使用委托的名称空间

UnityAction
替换所有with 。
Action``System

2。 全部

thisEvent.AddListener(listener);
替换为
thisEvent += listener;

3。 全部

thisEvent.RemoveListener(listener);
替换为
thisEvent -= listener;

这是Unity 原始 版本的修改版本,可

EventManager
移植为使用委托/动作。

不带参数:

using System;using System.Collections;using System.Collections.Generic;using UnityEngine;public class EventManager : MonoBehaviour{    private Dictionary<string, Action> 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<string, Action>();        }    }    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]();        }    }}

测试脚本:

下面的测试脚本通过每2秒触发一次事件来测试事件。

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!");    }}

带参数:

从其他问题来看,大多数人都在问如何支持参数。这里是。您可以使用

class
/
struct
作为参数,然后将要传递的所有变量添加到此类/结构中的函数中。我将
EventParam
举一个例子。随意
EventParam
在代码末尾添加/删除要在事件结构中传递的变量。

using System;using System.Collections;using System.Collections.Generic;using UnityEngine;public class EventManager : MonoBehaviour{    private Dictionary<string, Action<EventParam>> 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<string, Action<EventParam>>();        }    }    public static void StartListening(string eventName, Action<EventParam> listener)    {        Action<EventParam> 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<EventParam> listener)    {        if (eventManager == null) return;        Action<EventParam> 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<EventParam> 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 itpublic struct EventParam{    public string param1;    public int param2;    public float param3;    public bool param4;}

测试脚本:

public class Test : MonoBehaviour{    private Action<EventParam> someListener1;    private Action<EventParam> someListener2;    private Action<EventParam> someListener3;    void Awake()    {        someListener1 = new Action<EventParam>(SomeFunction);        someListener2 = new Action<EventParam>(SomeOtherFunction);        someListener3 = new Action<EventParam>(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!");    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373012.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号