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

 直接上代码~~~~  

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

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

    }
    private void OnDestroy()
    {
        MgrListener.Off(ListenerType.TEST1, TestFun1);
    }
    int count = 0;
    //广播
    public void OnClickBtn() //Send
    {
        MgrListener.Send(ListenerType.TEST1, "Send: Test1 "+ count);
        MgrListener.Send(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 arg1);
    public delegate void EventCallBack(T arg1, X arg2);
    public delegate void EventCallBack(T arg1, X arg2, Y arg3);
    public delegate void EventCallBack(T arg1, X arg2, Y arg3, Z arg4);
    public delegate void EventCallBack(T arg1, X arg2, Y arg3, Z arg4, W arg5);

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

MgrListener.cs 

 

using System;
using System.Collections.Generic;

    public class MgrListener
    {
        //所有监听的事件字典
        private static Dictionary _eventDic = new Dictionary();
        
       
        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(ListenerType eventType, EventCallBack callBack)
        {
            AddListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] + callBack;
        }
        public static void On(ListenerType eventType, EventCallBack callBack)
        {
            AddListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] + callBack;
        }
        public static void On(ListenerType eventType, EventCallBack callBack)
        {
            AddListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] + callBack;
        }
        public static void On(ListenerType eventType, EventCallBack callBack)
        {
            AddListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] + callBack;
        }
        public static void On(ListenerType eventType, EventCallBack callBack)
        {
            AddListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack)_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(ListenerType eventType, EventCallBack callBack)
        {
            AddListener(eventType, callBack);
            EventCallBack e = (arg) => { Off(eventType, callBack); };
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] + callBack + e;
        }
        public static void Once(ListenerType eventType, EventCallBack callBack)
        {
            AddListener(eventType, callBack);
            EventCallBack e = (arg1,arg2) => { Off(eventType, callBack); };
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] + callBack + e;
        }
        public static void Once(ListenerType eventType, EventCallBack callBack)
        {
            AddListener(eventType, callBack);
            EventCallBack e = (arg1, arg2, arg3) => { Off(eventType, callBack); };
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] + callBack + e;
        }
        public static void Once(ListenerType eventType, EventCallBack callBack)
        {
            AddListener(eventType, callBack);
            EventCallBack e = (arg1, arg2, arg3, arg4) => { Off(eventType, callBack); };

            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] + callBack + e;
        }
        public static void Once(ListenerType eventType, EventCallBack callBack)
        {
            AddListener(eventType, callBack);
            EventCallBack e = (arg1, arg2, arg3, arg4, arg5) => { Off(eventType, callBack); };
            _eventDic[eventType] = (EventCallBack)_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(ListenerType eventType, EventCallBack callBack)
        {
            RemoveListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] - callBack;
            OnRemoveListener(eventType);
        }
        public static void Off(ListenerType eventType, EventCallBack callBack)
        {
            RemoveListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] - callBack;
            OnRemoveListener(eventType);
        }
        public static void Off(ListenerType eventType, EventCallBack callBack)
        {
            RemoveListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] - callBack;
            OnRemoveListener(eventType);
        }
        public static void Off(ListenerType eventType, EventCallBack callBack)
        {
            RemoveListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack)_eventDic[eventType] - callBack;
            OnRemoveListener(eventType);
        }
        public static void Off(ListenerType eventType, EventCallBack callBack)
        {
            RemoveListener(eventType, callBack);
            _eventDic[eventType] = (EventCallBack)_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(ListenerType eventType, T arg)
        {
            Delegate d;
            if (_eventDic.TryGetValue(eventType, out d))
            {
                EventCallBack callBack = d as EventCallBack;
                callBack?.Invoke(arg);
            }
        }
        public static void Send(ListenerType eventType, T arg1, X arg2)
        {
            Delegate d;
            if (_eventDic.TryGetValue(eventType, out d))
            {
                EventCallBack callBack = d as EventCallBack;
                callBack?.Invoke(arg1, arg2);
            }
        }
        public static void Send(ListenerType eventType, T arg1, X arg2, Y arg3)
        {
            Delegate d;
            if (_eventDic.TryGetValue(eventType, out d))
            {
                EventCallBack callBack = d as EventCallBack;
                callBack?.Invoke(arg1, arg2, arg3);
            }
        }
        public static void Send(ListenerType eventType, T arg1, X arg2, Y arg3, Z arg4)
        {
            Delegate d;
            if (_eventDic.TryGetValue(eventType, out d))
            {
                EventCallBack callBack = d as EventCallBack;
                callBack?.Invoke(arg1, arg2, arg3, arg4);
            }
        }
        public static void Send(ListenerType eventType, T arg1, X arg2, Y arg3, Z arg4, W agr5)
        {
            Delegate d;
            if (_eventDic.TryGetValue(eventType, out d))
            {
                EventCallBack callBack = d as EventCallBack;
                callBack?.Invoke(arg1, arg2, arg3, arg4, agr5);
            }
        }
    }



 

你可能感兴趣的:(Unity)