UISystemEvent.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. public class UISystemEvent
  6. {
  7. public static Dictionary<UIEvent, UICallBack> s_allUIEvents = new Dictionary<UIEvent, UICallBack>();
  8. public static Dictionary<string, Dictionary<UIEvent, UICallBack>> s_singleUIEvents = new Dictionary<string, Dictionary<UIEvent, UICallBack>>();
  9. /// <summary>
  10. /// 每个UI都会派发的事件
  11. /// </summary>
  12. /// <param name="Event">事件类型</param>
  13. /// <param name="callback">回调函数</param>
  14. public static void RegisterAllUIEvent(UIEvent UIEvent, UICallBack CallBack)
  15. {
  16. if (s_allUIEvents.ContainsKey(UIEvent))
  17. {
  18. s_allUIEvents[UIEvent] += CallBack;
  19. }
  20. else
  21. {
  22. s_allUIEvents.Add(UIEvent,CallBack);
  23. }
  24. }
  25. public static void RemoveAllUIEvent(UIEvent UIEvent, UICallBack l_CallBack)
  26. {
  27. if (s_allUIEvents.ContainsKey(UIEvent))
  28. {
  29. s_allUIEvents[UIEvent] -= l_CallBack;
  30. }
  31. else
  32. {
  33. Debug.LogError("RemoveAllUIEvent don't exits: " + UIEvent);
  34. }
  35. }
  36. /// <summary>
  37. /// 注册单个UI派发的事件
  38. /// </summary>
  39. /// <param name="Event">事件类型</param>
  40. /// <param name="callback"回调函数></param>
  41. public static void RegisterEvent(string UIName,UIEvent UIEvent, UICallBack CallBack)
  42. {
  43. if (s_singleUIEvents.ContainsKey(UIName))
  44. {
  45. if (s_singleUIEvents[UIName].ContainsKey(UIEvent))
  46. {
  47. s_singleUIEvents[UIName][UIEvent] += CallBack;
  48. }
  49. else
  50. {
  51. s_singleUIEvents[UIName].Add(UIEvent,CallBack);
  52. }
  53. }
  54. else
  55. {
  56. s_singleUIEvents.Add(UIName,new Dictionary<UIEvent,UICallBack>());
  57. s_singleUIEvents[UIName].Add(UIEvent, CallBack);
  58. }
  59. }
  60. public static void RemoveEvent(string UIName, UIEvent UIEvent, UICallBack CallBack)
  61. {
  62. if (s_singleUIEvents.ContainsKey(UIName))
  63. {
  64. if (s_singleUIEvents[UIName].ContainsKey(UIEvent))
  65. {
  66. s_singleUIEvents[UIName][UIEvent] -= CallBack;
  67. }
  68. else
  69. {
  70. Debug.LogError("RemoveEvent 不存在的事件! UIName " + UIName + " UIEvent " + UIEvent);
  71. }
  72. }
  73. else
  74. {
  75. Debug.LogError("RemoveEvent 不存在的事件! UIName " + UIName + " UIEvent " + UIEvent);
  76. }
  77. }
  78. public static void Dispatch(UIWindowBase UI, UIEvent UIEvent,params object[] objs)
  79. {
  80. if (UI == null)
  81. {
  82. Debug.LogError("Dispatch l_UI is null!");
  83. return;
  84. }
  85. if (s_allUIEvents.ContainsKey(UIEvent))
  86. {
  87. try
  88. {
  89. if(s_allUIEvents[UIEvent] != null)
  90. s_allUIEvents[UIEvent](UI, objs);
  91. }
  92. catch (Exception e)
  93. {
  94. Debug.LogError("UISystemEvent Dispatch error:" + e.ToString());
  95. }
  96. }
  97. if (s_singleUIEvents.ContainsKey(UI.name))
  98. {
  99. if (s_singleUIEvents[UI.name].ContainsKey(UIEvent))
  100. {
  101. try
  102. {
  103. if (s_singleUIEvents[UI.name][UIEvent] != null)
  104. s_singleUIEvents[UI.name][UIEvent](UI, objs);
  105. }
  106. catch (Exception e)
  107. {
  108. Debug.LogError("UISystemEvent Dispatch error:" + e.ToString());
  109. }
  110. }
  111. }
  112. }
  113. }