using UnityEngine; using System.Collections; using System.Collections.Generic; /// 消息中心 public class MessageCenterController { #region 单例 private static MessageCenterController instance = new MessageCenterController(); private MessageCenterController() { } /// 单利对象 public static MessageCenterController Instance { get { return instance; } } #endregion /// /// 所有通用事件列表 /// private IDictionary eventList = new Dictionary(); /// 注册对应事件 /// 枚举标识 /// 通用委托 public void Register(string key, GlobalDeleg value) { if (eventList.ContainsKey(key)) eventList[key].Register(value); else { MessageDeleg eventDeleg = new MessageDeleg(); eventDeleg.Register(value); eventList.Add(key, eventDeleg); } } /// 注销对应事件 /// 枚举标识 /// 通用委托 public void UnRegister(string key, GlobalDeleg value) { if (eventList.ContainsKey (key)) eventList [key].UnRegister (value); else { //CDebug.LogWarning ("没有该通用事件可供注销!"); } } /// 广播对应事件 /// public void Broadcast(string key, object param = null) { if (eventList.ContainsKey (key)) eventList [key].Broadcast (param); else { //CDebug.LogWarning ("没有该通用事件可供广播!"); } } } #region 通用委托 /// 通用委托 /// 通用参数 public delegate void GlobalDeleg(object param = null); /// 事件中心委托 public class MessageDeleg { /// 通用事件 private event GlobalDeleg globalEvent = null; /// 注册事件 /// 通用委托 public void Register(GlobalDeleg eventDeleg) { this.globalEvent += eventDeleg; } /// 注销事件 /// 通用委托 public void UnRegister(GlobalDeleg eventDeleg) { this.globalEvent -= eventDeleg; } /// 广播事件 /// 通用参数 public void Broadcast(object param = null) { if (globalEvent != null) globalEvent(param); } } #endregion