MessageCenterController.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. /// <summary>消息中心</summary>
  5. public class MessageCenterController
  6. {
  7. #region 单例
  8. private static MessageCenterController instance = new MessageCenterController();
  9. private MessageCenterController() { }
  10. /// <summary>单利对象</summary>
  11. public static MessageCenterController Instance
  12. {
  13. get
  14. { return instance; }
  15. }
  16. #endregion
  17. /// <summary>
  18. /// 所有通用事件列表
  19. /// </summary>
  20. private IDictionary<string, MessageDeleg> eventList = new Dictionary<string, MessageDeleg>();
  21. /// <summary>注册对应事件</summary>
  22. /// <param name="key">枚举标识</param>
  23. /// <param name="value">通用委托</param>
  24. public void Register(string key, GlobalDeleg value)
  25. {
  26. if (eventList.ContainsKey(key))
  27. eventList[key].Register(value);
  28. else
  29. {
  30. MessageDeleg eventDeleg = new MessageDeleg();
  31. eventDeleg.Register(value);
  32. eventList.Add(key, eventDeleg);
  33. }
  34. }
  35. /// <summary>注销对应事件</summary>
  36. /// <param name="key">枚举标识</param>
  37. /// <param name="value">通用委托</param>
  38. public void UnRegister(string key, GlobalDeleg value)
  39. {
  40. if (eventList.ContainsKey (key))
  41. eventList [key].UnRegister (value);
  42. else {
  43. //CDebug.LogWarning ("没有该通用事件可供注销!");
  44. }
  45. }
  46. /// <summary>广播对应事件</summary>
  47. /// <param name="param"></param>
  48. public void Broadcast(string key, object param = null)
  49. {
  50. if (eventList.ContainsKey (key))
  51. eventList [key].Broadcast (param);
  52. else {
  53. //CDebug.LogWarning ("没有该通用事件可供广播!");
  54. }
  55. }
  56. }
  57. #region 通用委托
  58. /// <summary>通用委托</summary>
  59. /// <param name="param">通用参数</param>
  60. public delegate void GlobalDeleg(object param = null);
  61. /// <summary>事件中心委托 </summary>
  62. public class MessageDeleg
  63. {
  64. /// <summary>通用事件</summary>
  65. private event GlobalDeleg globalEvent = null;
  66. /// <summary>注册事件</summary>
  67. /// <param name="eventDeleg">通用委托</param>
  68. public void Register(GlobalDeleg eventDeleg)
  69. {
  70. this.globalEvent += eventDeleg;
  71. }
  72. /// <summary>注销事件</summary>
  73. /// <param name="eventDeleg">通用委托</param>
  74. public void UnRegister(GlobalDeleg eventDeleg)
  75. {
  76. this.globalEvent -= eventDeleg;
  77. }
  78. /// <summary>广播事件</summary>
  79. /// <param name="param">通用参数</param>
  80. public void Broadcast(object param = null)
  81. {
  82. if (globalEvent != null)
  83. globalEvent(param);
  84. }
  85. }
  86. #endregion