123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class MessageCenter
- {
- //委托消息传递
- public delegate void DelMessageDelivery(object kv);
- //消息中心缓存集合
- //<string:数据大的分类,DelMessageDelivery数据执行委托>
- public static Dictionary<object, DelMessageDelivery> _dicMessages = new Dictionary<object, DelMessageDelivery>();
- /// <summary>
- ///加消息的监听
- /// </summary>
- /// <param name="messageType">消息分类</param>
- /// <param name="handler">消息委托</param>
- public static void AddMsgListener(object messageType, DelMessageDelivery handler)
- {
- if(!_dicMessages.ContainsKey(messageType))
- {
- _dicMessages.Add(messageType, null);
- }
- _dicMessages[messageType] += handler;
- }
- /// <summary>
- /// 移除消息监听
- /// </summary>
- /// <param name="messageType">消息分类</param>
- /// <param name="handler">消息委托</param>
- public static void RemoveMsgListener(object messageType, DelMessageDelivery handler)
- {
- if(_dicMessages.ContainsKey(messageType))
- {
- _dicMessages[messageType] -= handler;
- }
- }
- /// <summary>
- /// 取消所有消息监听
- /// </summary>
- public static void ClearAllMsgListener()
- {
- if(_dicMessages != null)
- {
- _dicMessages.Clear();
- }
- }
- /// <summary>
- /// 发送消息
- /// </summary>
- /// <param name="messageType">消息分类</param>
- /// <param name="kv">键值对(对象)</param>
- public static void SendMessage(object messageType, object kv)
- {
- DelMessageDelivery del;
- if(_dicMessages.TryGetValue(messageType, out del))
- {
- if(del != null)
- {
- //调用委托
- del(kv);
- }
- }
- }
- }
- /// <summary>
- /// 配合委托,实现委托数据传递
- /// </summary>
- 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;
- }
- }
|