Unity 自定义事件(观察者模式)

 直接上代码~~~~  

 调用测试 On 监听 Once监听一次后不再监听  Off 移除 Send广播  

private void Awake()
    {
        MgrListener.On<string>(ListenerType.TEST1, TestFun1);
        MgrListener.Once<string>(ListenerType.TEST2, TestFun1);

    }
    private void OnDestroy()
    {
        MgrListener.Off<string>(ListenerType.TEST1, TestFun1);
    }
    int count = 0;
    //广播
    public void OnClickBtn() //Send
    {
        MgrListener.Send<string>(ListenerType.TEST1, "Send: Test1 "+ count);
        MgrListener.Send<string>(ListenerType.TEST2, "Send: Test2 "+ count);
        count++;
    }

   
    //回调方法
    public void TestFun1(string str) {

        Debug.Log("收到Fun1消息: " + str);
    
    }
    //回调方法 只会收到一次回调 Once
    public void TestFun2(string str)
    {
        Debug.Log("收到Fun2消息: " + str);

    }

ListenerType.cs 用来存储自定义Type

    public delegate void EventCallBack();
    public delegate void EventCallBack<T>(T arg1);
    public delegate void EventCallBack<T, X>(T arg1, X arg2);
    public delegate void EventCallBack<T, X, Y>(T arg1, X arg2, Y arg3);
    public delegate void EventCallBack<T, X, Y, Z>(T arg1, X arg2, Y arg3, Z arg4);
    public delegate void EventCallBack<T, X, Y, Z, W>(T arg1, X arg2, Y arg3, Z arg4, W arg5);

    /// <summary>
    /// 监听类型
    /// </summary>
    public enum ListenerType
    {
        NULL,
        TEST1,
        TEST2,
    }

MgrListener.cs 

using System;
using System.Collections.Generic;

    public class MgrListener
    {
        //所有监听的事件字典
        private static Dictionary<ListenerType, Delegate> _eventDic = new Dictionary<ListenerType, Delegate>();
        
       
        private static void AddListener(ListenerType eventType, Delegate callback)
        {
            //如果不存在 
            if (!_eventDic.ContainsKey(eventType))
            {
                //添加一个key 
                // val设置null 是因为可能会有多播 在后边有设置+=
                _eventDic.Add(eventType, null);
            }
            Delegate d = _eventDic[eventType];
            if (d != null && d.GetType() != callback.GetType())
            {
                throw new Exception(string.Format("尝试为事件{0}添加不同类型的委托{1}", eventType, callback.GetType()));
            }
        }

        private static void RemoveListener(ListenerType eventType, Delegate callback)
        {
            if (_eventDic.ContainsKey(eventType))
            {
                Delegate d = _eventDic[eventType];
                if (d == null)
                {
                    throw new Exception(string.Format("移除监听错误:事件{0}没有对应的委托", eventType));
                }
                else if (d.GetType() != callback.GetType())
                {
                    throw new Exception(string.Format("移除监听错误:尝试为事件{0}移除不同的类型的委托,移除的对象为{1}", eventType, callback));
                }
            }
            else
            {
                throw new Exception(string.Format("移除监听错误:{0}没有时间码", eventType));
            }
     }
        private static void OnRemoveListener(ListenerType eventType)
        {
            if (_eventDic[eventType] == null)
            {
                _eventDic.Remove(eventType);
            }
        }
        //以下是监听(多参)*************************************************************************************/

        public static void On(ListenerType eventType, EventCallBack callBack)
        {
            AddListener(eventType, callBack);
            //多播
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] + callBack;
        }
        public static void On<T>(ListenerType eventType, EventCallBack<T> callBack)
        {
            AddListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack<T>)_eventDic[eventType] + callBack;
        }
        public static void On<T, X>(ListenerType eventType, EventCallBack<T, X> callBack)
        {
            AddListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack<T, X>)_eventDic[eventType] + callBack;
        }
        public static void On<T, X, Y>(ListenerType eventType, EventCallBack<T, X, Y> callBack)
        {
            AddListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack<T, X, Y>)_eventDic[eventType] + callBack;
        }
        public static void On<T, X, Y, Z>(ListenerType eventType, EventCallBack<T, X, Y, Z> callBack)
        {
            AddListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack<T, X, Y, Z>)_eventDic[eventType] + callBack;
        }
        public static void On<T, X, Y, Z, W>(ListenerType eventType, EventCallBack<T, X, Y, Z, W> callBack)
        {
            AddListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack<T, X, Y, Z, W>)_eventDic[eventType] + callBack;
        }
        //以下是只监听一次(此事件不用在Destory移除)*************************************************************************************/
        public static void Once(ListenerType eventType, EventCallBack callBack)
        {
            AddListener(eventType, callBack);
            //播出后立即移除
            EventCallBack e = () => { Off(eventType, callBack); };
            //尾部增加移除方法
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] + callBack + e;       
        }
        public static void Once<T>(ListenerType eventType, EventCallBack<T> callBack)
        {
            AddListener(eventType, callBack);
            EventCallBack<T> e = (arg) => { Off(eventType, callBack); };
            _eventDic[eventType] = (EventCallBack<T>)_eventDic[eventType] + callBack + e;
        }
        public static void Once<T, X>(ListenerType eventType, EventCallBack<T, X> callBack)
        {
            AddListener(eventType, callBack);
            EventCallBack<T,X> e = (arg1,arg2) => { Off(eventType, callBack); };
            _eventDic[eventType] = (EventCallBack<T, X>)_eventDic[eventType] + callBack + e;
        }
        public static void Once<T, X, Y>(ListenerType eventType, EventCallBack<T, X, Y> callBack)
        {
            AddListener(eventType, callBack);
            EventCallBack<T, X,Y> e = (arg1, arg2, arg3) => { Off(eventType, callBack); };
            _eventDic[eventType] = (EventCallBack<T, X, Y>)_eventDic[eventType] + callBack + e;
        }
        public static void Once<T, X, Y, Z>(ListenerType eventType, EventCallBack<T, X, Y, Z> callBack)
        {
            AddListener(eventType, callBack);
            EventCallBack<T, X, Y,Z> e = (arg1, arg2, arg3, arg4) => { Off(eventType, callBack); };

            _eventDic[eventType] = (EventCallBack<T, X, Y, Z>)_eventDic[eventType] + callBack + e;
        }
        public static void Once<T, X, Y, Z, W>(ListenerType eventType, EventCallBack<T, X, Y, Z, W> callBack)
        {
            AddListener(eventType, callBack);
            EventCallBack<T, X, Y, Z,W> e = (arg1, arg2, arg3, arg4, arg5) => { Off(eventType, callBack); };
            _eventDic[eventType] = (EventCallBack<T, X, Y, Z, W>)_eventDic[eventType] + callBack + e;
        }


        //以下是移除监听*************************************************************************************/

        public static void Off(ListenerType eventType, EventCallBack callBack)
        {
            RemoveListener(eventType, callBack);
            //移除多播
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] - callBack;
            OnRemoveListener(eventType);
        }
        public static void Off<T>(ListenerType eventType, EventCallBack<T> callBack)
        {
            RemoveListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack<T>)_eventDic[eventType] - callBack;
            OnRemoveListener(eventType);
        }
        public static void Off<T, X>(ListenerType eventType, EventCallBack<T, X> callBack)
        {
            RemoveListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack<T, X>)_eventDic[eventType] - callBack;
            OnRemoveListener(eventType);
        }
        public static void Off<T, X, Y>(ListenerType eventType, EventCallBack<T, X, Y> callBack)
        {
            RemoveListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack<T, X, Y>)_eventDic[eventType] - callBack;
            OnRemoveListener(eventType);
        }
        public static void Off<T, X, Y, Z>(ListenerType eventType, EventCallBack<T, X, Y, Z> callBack)
        {
            RemoveListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack<T, X, Y, Z>)_eventDic[eventType] - callBack;
            OnRemoveListener(eventType);
        }
        public static void Off<T, X, Y, Z, W>(ListenerType eventType, EventCallBack<T, X, Y, Z, W> callBack)
        {
            RemoveListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack<T, X, Y, Z, W>)_eventDic[eventType] - callBack;
            OnRemoveListener(eventType);
        }
        //以下广播(多参)*************************************************************************************/
        public static void Send(ListenerType eventType)
        {
            Delegate d;
            if (_eventDic.TryGetValue(eventType, out d))
            {
                EventCallBack callBack = d as EventCallBack;
                callBack?.Invoke();
            }
        }
        public static void Send<T>(ListenerType eventType, T arg)
        {
            Delegate d;
            if (_eventDic.TryGetValue(eventType, out d))
            {
                EventCallBack<T> callBack = d as EventCallBack<T>;
                callBack?.Invoke(arg);
            }
        }
        public static void Send<T, X>(ListenerType eventType, T arg1, X arg2)
        {
            Delegate d;
            if (_eventDic.TryGetValue(eventType, out d))
            {
                EventCallBack<T, X> callBack = d as EventCallBack<T, X>;
                callBack?.Invoke(arg1, arg2);
            }
        }
        public static void Send<T, X, Y>(ListenerType eventType, T arg1, X arg2, Y arg3)
        {
            Delegate d;
            if (_eventDic.TryGetValue(eventType, out d))
            {
                EventCallBack<T, X, Y> callBack = d as EventCallBack<T, X, Y>;
                callBack?.Invoke(arg1, arg2, arg3);
            }
        }
        public static void Send<T, X, Y, Z>(ListenerType eventType, T arg1, X arg2, Y arg3, Z arg4)
        {
            Delegate d;
            if (_eventDic.TryGetValue(eventType, out d))
            {
                EventCallBack<T, X, Y, Z> callBack = d as EventCallBack<T, X, Y, Z>;
                callBack?.Invoke(arg1, arg2, arg3, arg4);
            }
        }
        public static void Send<T, X, Y, Z, W>(ListenerType eventType, T arg1, X arg2, Y arg3, Z arg4, W agr5)
        {
            Delegate d;
            if (_eventDic.TryGetValue(eventType, out d))
            {
                EventCallBack<T, X, Y, Z, W> callBack = d as EventCallBack<T, X, Y, Z, W>;
                callBack?.Invoke(arg1, arg2, arg3, arg4, agr5);
            }
        }
    }



发布了40 篇原创文章 · 获赞 36 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/yzx5452830/article/details/105055298