unity 事件分发器

在大一点的项目里时间分发器是很必要的,我直接上干货代码


//事件分发基类
using System;
using System.Collections.Generic;
namespace Logic.Base
{
    public class MsgDispatcher<expand>where expand :class,new()
    {
        public delegate void Handler(expand param);
        private Dictionary<int, List<Handler>> _handlers = new Dictionary<int, List<Handler>>();


        public void Dispatch<CmdT>(CmdT cmd, expand mb = null)
        {
            List<Handler> list;
            int key = Convert.ToInt32(cmd);
            this._handlers.TryGetValue(key, out list);
            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    list[i](mb);
                }
            }
        }


        public void Register<CmdT>(CmdT cmd, Handler handler)
        {
            List<Handler> list;
            int key = Convert.ToInt32(cmd);
            if (!this._handlers.ContainsKey(key))
            {
                list = new List<Handler>();
                this._handlers[key] = list;
            }
            else
            {
                list = this._handlers[key];
            }
            if (list.Find(it => it == handler) == null)
            {
                list.Add(handler);
            }
        }


        public void Remove<CmdT>(CmdT cmd, Handler handler)
        {
            int key = Convert.ToInt32(cmd);
            if (this._handlers.ContainsKey(key))
            {
                this._handlers[key].Remove(handler);
            }
        }
    }
}


//事件分发管理基类

 using System;
 using System.Collections.Generic;


namespace Logic.Base
{
    public class MsgDispatcherMgr<expand> where expand:class,new()
    {
        private MsgDispatcher<expand> _generalMsgDispatcher = new MsgDispatcher<expand>();
        private Dictionary<ulong, MsgDispatcher<expand>> _specificMsgDispatchers = new Dictionary<ulong, MsgDispatcher<expand>>();


        public MsgDispatcher<expand> AddMsgDispatcher(ulong key)
        {
            MsgDispatcher<expand> dispatcher;
            if (!this._specificMsgDispatchers.TryGetValue(key, out dispatcher))
            {
                dispatcher = new MsgDispatcher<expand>();
                this._specificMsgDispatchers[key] = dispatcher;
            }
            return dispatcher;
        }


        public void Dispatch<CmdT>(CmdT cmd, expand mb = null)
        {
            this._generalMsgDispatcher.Dispatch(cmd, mb);
        }
        public void DispatchWithKey<CmdT>(ulong key, CmdT cmd, expand mb = null)
        {
            MsgDispatcher<expand> dispatcher;
            if (this._specificMsgDispatchers.TryGetValue(key, out dispatcher))
            {
                dispatcher.Dispatch(cmd, mb);
            }
        }


        public void Register<CmdT>(CmdT cmd, MsgDispatcher<expand>.Handler handler)
        {
            this._generalMsgDispatcher.Register<CmdT>(cmd, handler);
        }


        public void Register<CmdT>(ulong key, CmdT cmd, MsgDispatcher<expand>.Handler handler)
        {
            this.AddMsgDispatcher(key).Register<CmdT>(cmd, handler);
        }


        public void Remove<CmdT>(CmdT cmd, MsgDispatcher<expand>.Handler handler)
        {
            this._generalMsgDispatcher.Remove<CmdT>(cmd, handler);
        }


        public void Remove<CmdT>(ulong key, CmdT cmd, MsgDispatcher<expand>.Handler handler)
        {
            MsgDispatcher<expand> dispatcher;
            if (this._specificMsgDispatchers.TryGetValue(key, out dispatcher))
            {
                dispatcher.Remove<CmdT>(cmd, handler);
            }
        }
    }
}

//事件分发 管理类 继承事件分发 管理基类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Logic.Base
{
    public class UiDispatcherMgr:BaseSingle<MsgDispatcherMgr<object>>
    {


    }
    public class CommonDispatcherMgr : BaseSingle<MsgDispatcherMgr<object>>
    {


    }

}


//这里增加一个用法

//这一句事件注册 

//UiDispatcherMgr.Instance.Register(BaseEventType.E_Refresh_Common_Ui, Test);

//已注册事件移除

//UiDispatcherMgr.Instance.Remove(BaseEventType.E_Refresh_Common_Ui, Test);

//事件分发

UiDispatcherMgr.Instance.Dispatch(BaseEventType.E_Refresh_Common_Ui);
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值