Unity 事件统一管理

来源:互联网 发布:老a淘宝工具箱 编辑:程序博客网 时间:2024/05/22 01:38
using System;using System.Collections;using System.Collections.Generic;public class DispatchEvent : IDispatchEvent{    Dictionary<string, List<Action<MyEvent>>> eventDic = new Dictionary<string, List<Action<MyEvent>>>();  public static DispatchEvent Instance    {        get {            if (instance == null)            {                instance = new DispatchEvent();            }            return instance;        }    }    private static DispatchEvent instance;    public void AddEventListener(string type, Action<MyEvent> addEvent)    {        if (HasEventListener(type,addEvent) == false )        {            return;        }        if (eventDic.ContainsKey(type) == false )        {            eventDic.Add(type,new List<Action<MyEvent>>());        }        eventDic[type].Add(addEvent);    }    public bool HasEventListener(string type, Action<MyEvent> hasEvent)    {        if (eventDic.ContainsKey(type) == true )        {            return false;        }        return true;    }    public void RemoveEventListener(string type, Action<MyEvent> removeEvent)    {        if (HasEventListener(type, removeEvent) == false)        {            return;        }        eventDic[type].Remove(removeEvent);        if (eventDic[type].Count == 0)        {            eventDic.Remove(type);        }    }    public void dispatchEvent(MyEvent myEvent)    {        if (eventDic.ContainsKey(myEvent.type) == false )        {            return;        }        List<Action<MyEvent>> tempEvent = eventDic[myEvent.type];        for (int i = 0; i < tempEvent.Count; i++)        {            tempEvent[i](myEvent);        }    }}public class MyEvent{    public string type;    public object data;    public MyEvent(string type,object data = null)    {        this.type = type;        this.data = data;    }}public interface IDispatchEvent{    void AddEventListener(string type,Action<MyEvent> addEvent);    void RemoveEventListener(string type,Action<MyEvent> removeEvent);    bool HasEventListener(string type ,Action<MyEvent> hasEvent);    void dispatchEvent(MyEvent myEvent);}

事件类型 [type] 事件 参数[data] 添加事件(AddEventListener) 移除事件(RemoveEventListener )
检查事件(HasEventListener) 抛掷事件(dispatchEvent) 所有事件容器(eventDic)

测试:

public class App : MonoBehaviour{    public void Awake()    {        DispatchEvent.Instance.AddEventListener(TestDispatchEvent.testDispatchEvent, TestEvent);    }    void Start()    {        DispatchEvent.Instance.dispatchEvent(new TestDispatchEvent(TestDispatchEvent.testDispatchEvent));    }    void TestEvent(MyEvent myEvent)    {        Debug.Log("TestEvent");    }}public class TestDispatchEvent:MyEvent{    // 事件类型    public static string testDispatchEvent = "testDispatchEvent";    public TestDispatchEvent(string type, object data = null) : base(type, data)    {    }}
DispatchEvent.Instance.AddEventListener(TestDispatchEvent.testDispatchEvent, TestEvent);

哎呀:添加一个事件要这么多代码 ,太麻烦,把它结合到单例里面
添加前:
public class Singleton : Iinstance where T:Iinstance,new()
添加后
public class Singleton : DispatchEvent, Iinstance where T:Iinstance,new() {}

调用直接 this.AddEventListener(); this.dispatchEvent();this.RemoveEventListener();

0 0
原创粉丝点击