事件管理器

使用

   ListenMgr.GetIns().Register(EventType.OnLevelUp, OnLvUpCB);
   ListenMgr.GetIns().Trigger(EventType.OnLevelUp);

   ListenMgr.GetIns().Register<int>(EventType.OnBloodChange, OnBloodChangeCB);
   ListenMgr.GetIns().Trigger<int>(EventType.OnBloodChange, 100);

回调

        private static void OnLvUpCB()
        {
            Console.WriteLine("CB");
        }
        private static void OnBloodChangeCB(int changeValue)
        {
            Console.WriteLine(changeValue);
        }

事件管理器

    public enum EventType
    {
        OnLevelUp,
        OnBloodChange
    }

    public class ListenMgr
    {
        public delegate void Callback();
        public delegate void Callback<T>(T arg1);
        public delegate void Callback<T, U>(T arg1, U arg2);
        public delegate void Callback<T, U, V>(T arg1, U arg2, V arg3);

        private Dictionary<EventType, Delegate> m_dicEvents = new Dictionary<EventType, Delegate>();

        #region Singleton

        private static ListenMgr listenMgr;
        private ListenMgr() { }
        public static ListenMgr GetIns()
        {
            if (listenMgr == null)
                listenMgr = new ListenMgr();
            return listenMgr;
        }

        #endregion

        #region Register

        public void Register(EventType eventType, Callback handler)
        {
            if (!m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = null;
            m_dicEvents[eventType] = (Callback)m_dicEvents[eventType] + handler;
            /* 委托源码 *
            this.m_dicEvents[eventType] = (Callback) Delegate.Combine((Callback) this.m_dicEvents[eventType], handler);
            [__DynamicallyInvokable]
            public static Delegate Combine(Delegate a, Delegate b)
            {
                if (a == null)
                {
                    return b;
                }
                return a.CombineImpl(b);
            }
             * */
        }
        public void Register<T>(EventType eventType, Callback<T> handler)
        {
            if (!m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = null;
            m_dicEvents[eventType] = (Callback<T>)m_dicEvents[eventType] + handler;
        }
        public void Register<T, K>(EventType eventType, Callback<T, K> handler)
        {
            if (!m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = null;
            m_dicEvents[eventType] = (Callback<T, K>)m_dicEvents[eventType] + handler;
        }
        public void Register<T, K, U>(EventType eventType, Callback<T, K, U> handler)
        {
            if (!m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = null;
            m_dicEvents[eventType] = (Callback<T, K, U>)m_dicEvents[eventType] + handler;
        }

        #endregion

        #region Unregister

        public void Unregister(EventType eventType, Callback handler)
        {
            if (m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = (Callback)m_dicEvents[eventType] - handler;
        }
        public void Unregister<T>(EventType eventType, Callback<T> handler)
        {
            if (m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = (Callback<T>)m_dicEvents[eventType] - handler;
        }
        public void Unregister<T, K>(EventType eventType, Callback<T, K> handler)
        {
            if (m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = (Callback<T, K>)m_dicEvents[eventType] - handler;
        }
        public void Unregister<T, K, U>(EventType eventType, Callback<T, K, U> handler)
        {
            if (m_dicEvents.ContainsKey(eventType))
                m_dicEvents[eventType] = (Callback<T, K, U>)m_dicEvents[eventType] - handler;
        }

        #endregion

        #region Trigger

        public void Trigger(EventType eventType)
        {
            Delegate d;
            if (m_dicEvents.TryGetValue(eventType, out d))
            {
                Callback callback = d as Callback;
                if (callback != null)
                    callback();
            }
        }
        public void Trigger<T>(EventType eventType, T args)
        {
            Delegate d;
            if (m_dicEvents.TryGetValue(eventType, out d))
            {
                Callback<T> callback = d as Callback<T>;
                if (callback != null)
                    callback(args);
            }
        }
        public void Trigger<T, K>(EventType eventType, T argst, K agrsu)
        {
            Delegate d;
            if (m_dicEvents.TryGetValue(eventType, out d))
            {
                Callback<T, K> callback = d as Callback<T, K>;
                if (callback != null)
                    callback(argst, agrsu);
            }
        }
        public void Trigger<T, K, U>(EventType eventType, T argst, K argsk, U argsu)
        {
            Delegate d;
            if (m_dicEvents.TryGetValue(eventType, out d))
            {
                Callback<T, K, U> callback = d as Callback<T, K, U>;
                if (callback != null)
                    callback(argst, argsk, argsu);
            }
        }

        #endregion
    }

 

转载于:https://www.cnblogs.com/HelloUnity/p/5754324.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值