// **********************************************************************// Copyright (C) XM// Author: 吴肖牧// Date: 2018-04-13// Desc: 消息的类型// **********************************************************************/// /// 消息的类型/// public enum MessageType{/// /// 启动/// START_UP = 1000,/// /// 解压/// UNPACK,/// /// 更新/// UPDATE,/// /// 更新完成/// UPDATE_COMPLETE,}/// /// 战斗的类型/// public enum BattleEvent{/// /// 攻击/// Attack = 10000,}/// /// 协议的类型/// public enum ProtocolEvent{}
4.然后我们定义一下消息派发者的接口 。
// **********************************************************************// Copyright (C) XM// Author: 吴肖牧// Date: 2018-04-13// Desc: // **********************************************************************using System.Collections;using System.Collections.Generic;using UnityEngine;public interface IDispatcher{void AddListener(int type, EventListenerDelegate listener);void RemoveListener(int type, EventListenerDelegate listener);void SendMessage(Message evt);void SendMessage(int type, params System.Object[] param);void Clear();}
5.接口的实现 。
// **********************************************************************// Copyright (C) XM// Author: 吴肖牧// Date: 2018-04-13// Desc: // **********************************************************************using System;using System.Collections;using System.Collections.Generic;using UnityEngine;public delegate void EventListenerDelegate(Message evt);public class Dispatcher : Singleton, IDispatcher{private Dictionary events = new Dictionary();public void AddListener(int type, EventListenerDelegate listener){if (listener == null){XMDebug.LogError("AddListener: listener不能为空");return;}EventListenerDelegate myListener = null;events.TryGetValue(type, out myListener);events[type] = (EventListenerDelegate)Delegate.Combine(myListener, listener);}public void RemoveListener(int type, EventListenerDelegate listener){if (listener == null){XMDebug.LogError("RemoveListener: listener不能为空");return;}events[type] = (EventListenerDelegate)Delegate.Remove(events[type], listener);}public void Clear(){events.Clear();}public void SendMessage(Message evt){EventListenerDelegate listenerDelegate;if (events.TryGetValue(evt.Type, out listenerDelegate)){try{if (listenerDelegate != null){listenerDelegate(evt);}}catch (System.Exception e){XMDebug.LogError("SendMessage:", evt.Type.ToString(), e.Message, e.StackTrace, e);}}}public void SendMessage(int type, params System.Object[] param){EventListenerDelegate listenerDelegate;if (events.TryGetValue(type, out listenerDelegate)){Message evt = new Message(type, param);try{if (listenerDelegate != null){listenerDelegate(evt);}}catch (System.Exception e){XMDebug.LogError("SendMessage:", evt.Type.ToString(), e.Message, e.StackTrace, e);}}}public void AddListener(MessageType type, EventListenerDelegate listener){AddListener((int)type, listener);}public void AddListener(BattleEvent type, EventListenerDelegate listener){AddListener((int)type, listener);}public void AddListener(ProtocolEvent type, EventListenerDelegate listener){AddListener((int)type, listener);}public void RemoveListener(MessageType type, EventListenerDelegate listener){RemoveListener((int)type, listener);}public void RemoveListener(BattleEvent type, EventListenerDelegate listener){RemoveListener((int)type, listener);}public void RemoveListener(ProtocolEvent type, EventListenerDelegate listener){RemoveListener((int)type, listener);}public void SendMessage(MessageType type, params System.Object[] param){SendMessage((int)type, param);}public void SendMessage(BattleEvent type, params System.Object[] param){SendMessage((int)type, param);}public void SendMessage(ProtocolEvent type, params System.Object[] param){SendMessage((int)type, param);}}