• 主页

  • 投资

  • IT

    🔥
  • 设计

  • 销售

  • 共151篇

    Unity - 游戏引擎

关闭

返回栏目

关闭

返回Unity - 游戏引擎栏目

136 - EventCenter.cs - 事件中心

作者:

贺及楼

成为作者

更新日期:2023-09-17 11:01:00

EventCenter.cs

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. //里氏转换的空接口
  6. public interface IEventInfo { }
  7. public class EventInfo<T> : IEventInfo
  8. {
  9. public UnityAction<T> actions;
  10. public EventInfo(UnityAction<T> action)
  11. {
  12. actions += action;
  13. }
  14. }
  15. public class EventInfo : IEventInfo
  16. {
  17. public UnityAction actions;
  18. public EventInfo(UnityAction action)
  19. {
  20. actions += action;
  21. }
  22. }
  23. //事件中心
  24. public class EventCenter : BaseManager<EventCenter>
  25. {
  26. private Dictionary<string, IEventInfo> eventDic = new Dictionary<string, IEventInfo>();
  27. /// <summary>
  28. /// 添加事件监听+泛型
  29. /// </summary>
  30. /// <param name="name"></param>
  31. /// <param name="action"></param>
  32. public void AddEventListener<T>(string name, UnityAction<T> action)
  33. {
  34. //字典有
  35. if (eventDic.ContainsKey(name))
  36. {
  37. (eventDic[name] as EventInfo<T>).actions += action;
  38. }
  39. //无
  40. else
  41. {
  42. eventDic.Add(name, new EventInfo<T>(action));
  43. }
  44. }
  45. /// <summary>
  46. /// 添加事件监听 无泛型
  47. /// </summary>
  48. /// <param name="name"></param>
  49. /// <param name="action"></param>
  50. public void AddEventListener(string name, UnityAction action)
  51. {
  52. //字典有
  53. if (eventDic.ContainsKey(name))
  54. {
  55. (eventDic[name] as EventInfo).actions += action;
  56. }
  57. //无
  58. else
  59. {
  60. eventDic.Add(name, new EventInfo(action));
  61. }
  62. }
  63. /// <summary>
  64. /// 移除监听+泛型
  65. /// </summary>
  66. /// <param name="name"></param>
  67. /// <param name="action"></param>
  68. public void RemoveEventListener<T>(string name, UnityAction<T> action)
  69. {
  70. if (eventDic.ContainsKey(name))
  71. {
  72. (eventDic[name] as EventInfo<T>).actions -= action;
  73. }
  74. }
  75. /// <summary>
  76. /// 移除监听 无泛型
  77. /// </summary>
  78. /// <param name="name"></param>
  79. /// <param name="action"></param>
  80. public void RemoveEventListener(string name, UnityAction action)
  81. {
  82. if (eventDic.ContainsKey(name))
  83. {
  84. (eventDic[name] as EventInfo).actions -= action;
  85. }
  86. }
  87. /// <summary>
  88. /// 事件触发+泛型
  89. /// </summary>
  90. /// <param name="name"></param>
  91. /// <param name="info"></param>
  92. public void EventTrigger<T>(string name, T info)
  93. {
  94. //字典有
  95. if (eventDic.ContainsKey(name))
  96. {
  97. //eventDic[name]();
  98. //eventDic[name].Invoke(info);
  99. //
  100. if ((eventDic[name] as EventInfo<T>).actions != null)
  101. {
  102. (eventDic[name] as EventInfo<T>).actions.Invoke(info);
  103. }
  104. }
  105. }
  106. /// <summary>
  107. /// 事件触发 无泛型
  108. /// </summary>
  109. /// <param name="name"></param>
  110. /// <param name="info"></param>
  111. public void EventTrigger(string name)
  112. {
  113. //字典有
  114. if (eventDic.ContainsKey(name))
  115. {
  116. //eventDic[name]();
  117. //eventDic[name].Invoke(info);
  118. //
  119. if ((eventDic[name] as EventInfo).actions != null)
  120. {
  121. (eventDic[name] as EventInfo).actions.Invoke();
  122. }
  123. }
  124. }
  125. public void check()
  126. {
  127. Debug.Log(eventDic.Count);
  128. }
  129. //清空事件中心主要在场景切换
  130. public void Clear()
  131. {
  132. eventDic.Clear();
  133. }
  134. }