Unity中EventManager的实现方式

在进行Unity相关开发过程中我们总是需要响应很多的事件,如果没有一个集中的管理,代码就会变得很混乱,这时候EventManager就变的很重要了。

这里根据Unity的官方教程:Events: Creating a simple messaging system,通过调用Unity的时间系统来进行封装。(留下备用)

这里是Unity官方教程的实现方式,这里改成了单例模式。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class EventManager
{
    private Dictionary<string, UnityEvent> eventDictionary = new Dictionary<string, UnityEvent>();
    private static EventManager eventManager = new EventManager();
    private EventManager()
    {

    }
    public static EventManager GetInstance
    {
        get
        {
            return eventManager;
        }
    }
    public void StartListening(string eventName, UnityAction listener)
    {
        UnityEvent thisEvent = null;
        if (eventManager.eventDictionary.TryGetValue(eventName, out thisEvent))
        {
            thisEvent.AddListener(listener);
        }
        else
        {
            thisEvent = new UnityEvent();
            thisEvent.AddListener(listener);
            eventManager.eventDictionary.Add(eventName, thisEvent);
        }
    }

    public void StopListening(string eventName, UnityAction listener)
    {
        if (eventManager == null) return;
        UnityEvent thisEvent = null;
        if (eventManager.eventDictionary.TryGetValue(eventName, out thisEvent))
        {
            thisEvent.RemoveListener(listener);
        }
    }

    public void TriggerEvent(string eventName)
    {
        UnityEvent thisEvent = null;
        if (eventManager.eventDictionary.TryGetValue(eventName, out thisEvent))
        {
            thisEvent.Invoke();
        }
    }
}

事件注册:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

enum EventEnum
{
    start,
    Active,
    end,
    unregistered
}

public class EventTest : MonoBehaviour
{
    void Start()
    {
        EventManager.GetInstance.TriggerEvent(EventEnum.start.ToString());
    }

    private void OnEnable()
    {
        EventManager.GetInstance.StartListening(EventEnum.start.ToString(), StartGame);
        EventManager.GetInstance.StartListening(EventEnum.Active.ToString(), ActiveGame);
        EventManager.GetInstance.StartListening(EventEnum.end.ToString(), EndGame);
    }

    public void OnDisable()
    {
        EventManager.GetInstance.StopListening(EventEnum.start.ToString(), StartGame);
        EventManager.GetInstance.StopListening(EventEnum.Active.ToString(), ActiveGame);
        EventManager.GetInstance.StopListening(EventEnum.end.ToString(), EndGame);
    }

    public void StartGame()
    {
        Debug.Log("----------->>>>StartGame");
    }

    public void ActiveGame()
    {
        Debug.Log("----------->>>>ActiveGame");
    }

    public void EndGame()
    {
        Debug.Log("----------->>>>EndGame");
    }
}

事件触发:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EventTrigger : MonoBehaviour
{
    void Start()
    {
        
    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
        {
            EventManager.GetInstance.TriggerEvent(EventEnum.Active.ToString());
        }
        if (Input.GetKeyDown(KeyCode.S))
        {
            EventManager.GetInstance.TriggerEvent(EventEnum.end.ToString());
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            EventManager.GetInstance.TriggerEvent(EventEnum.start.ToString());
        }
        if (Input.GetKeyDown(KeyCode.W))
        {
            EventManager.GetInstance.TriggerEvent(EventEnum.unregistered.ToString());
        }
    }
}

注:在事件触发这里,我触发了一个没有被监听的事件,既没有相应也没有错误,开发时需要注意。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章