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