using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class MessageCenter { //委托消息传递 public delegate void DelMessageDelivery(object kv); //消息中心缓存集合 // public static Dictionary _dicMessages = new Dictionary(); /// ///加消息的监听 /// /// 消息分类 /// 消息委托 public static void AddMsgListener(object messageType, DelMessageDelivery handler) { if(!_dicMessages.ContainsKey(messageType)) { _dicMessages.Add(messageType, null); } _dicMessages[messageType] += handler; } /// /// 移除消息监听 /// /// 消息分类 /// 消息委托 public static void RemoveMsgListener(object messageType, DelMessageDelivery handler) { if(_dicMessages.ContainsKey(messageType)) { _dicMessages[messageType] -= handler; } } /// /// 取消所有消息监听 /// public static void ClearAllMsgListener() { if(_dicMessages != null) { _dicMessages.Clear(); } } /// /// 发送消息 /// /// 消息分类 /// 键值对(对象) public static void SendMessage(object messageType, object kv) { DelMessageDelivery del; if(_dicMessages.TryGetValue(messageType, out del)) { if(del != null) { //调用委托 del(kv); } } } } /// /// 配合委托,实现委托数据传递 /// public class KeyValuesUpdate { //键 private string _Key; //值 private object _Values; //只读属性 public string Key { get { return _Key; } } public object Values { get { return _Values; } } public KeyValuesUpdate(string key, object valueObj) { _Key = key; _Values = valueObj; } }