在大一点的项目里时间分发器是很必要的,我直接上干货代码
//事件分发基类
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);