EventManager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using SC.XR.Unity;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class EventManager : SingletonMono<EventManager>
  6. {
  7. private Dictionary<EventID, List<EventObserver>> m_ObserList = new Dictionary<EventID, List<EventObserver>>();
  8. private Queue m_Quene = new Queue();
  9. private void Update()
  10. {
  11. while (m_Quene.Count > 0)
  12. {
  13. EventData data = (EventData)m_Quene.Dequeue();
  14. if (!m_ObserList.ContainsKey(data.ID))
  15. {
  16. continue;
  17. }
  18. List<EventObserver> observers = m_ObserList[data.ID];
  19. for (int i = 0; i < observers.Count; i++)
  20. {
  21. if (observers[i] == null)
  22. {
  23. continue;
  24. }
  25. observers[i].HandleEvent(data);
  26. }
  27. }
  28. }
  29. //发送事件
  30. public void SendEvent(EventData eve)
  31. {
  32. m_Quene.Enqueue(eve);
  33. }
  34. /// <summary>
  35. /// 添加监听者
  36. /// </summary>
  37. /// <param name="newobj">监听者</param>
  38. /// <param name="eid">监听的事件</param>
  39. private void RegisterObj(EventObserver newobj, EventID eid)
  40. {
  41. if (!m_ObserList.ContainsKey(eid))
  42. {
  43. List<EventObserver> list = new List<EventObserver>();
  44. list.Add(newobj);
  45. m_ObserList.Add(eid, list);
  46. }
  47. else
  48. {
  49. List<EventObserver> list = m_ObserList[eid];
  50. foreach (EventObserver obj in list)
  51. {
  52. if (obj == newobj)
  53. {
  54. return;
  55. }
  56. }
  57. list.Add(newobj);
  58. }
  59. }
  60. /// <summary>
  61. /// 移除监听者
  62. /// </summary>
  63. /// <param name="removeobj">监听者</param>
  64. private void RemoveObj(EventObserver removeobj)
  65. {
  66. foreach (KeyValuePair<EventID, List<EventObserver>> kv in m_ObserList)
  67. {
  68. List<EventObserver> list = kv.Value;
  69. foreach (EventObserver obj in list)
  70. {
  71. if (obj == removeobj)
  72. {
  73. list.Remove(obj);
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// 移除一个监听者
  81. /// </summary>
  82. /// <param name="removeobj">监听者</param>
  83. public void Remove(EventObserver removeobj)
  84. {
  85. if (EventManager.Instance == null) return;
  86. EventManager.Instance.RemoveObj(removeobj);
  87. }
  88. /// <summary>
  89. /// 监听者在这里注册
  90. /// </summary>
  91. /// <param name="newobj">监听者</param>
  92. /// <param name="eids">需要监听的事件列表.</param>
  93. public void Register(EventObserver newobj, params EventID[] eids)
  94. {
  95. if (EventManager.Instance == null) return;
  96. foreach (EventID eid in eids)
  97. {
  98. EventManager.Instance.RegisterObj(newobj, eid);
  99. }
  100. }
  101. private void on()
  102. {
  103. //m_Quene.Clear();
  104. }
  105. private void OnApplicationQuit()
  106. {
  107. m_ObserList.Clear();
  108. }
  109. }
  110. public enum EventID
  111. {
  112. /// <summary>
  113. /// 选中素材时触发事件
  114. /// </summary>
  115. EVENT_1 = 10001,
  116. EVENT_2 = 10002,
  117. /// <summary>
  118. /// 选中锚点
  119. /// </summary>
  120. EVENT_3 = 10003,
  121. }
  122. public interface EventObserver
  123. {
  124. void HandleEvent(EventData resp);
  125. }