EventHandlerItem.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace ProjectBase.Event
  5. {
  6. public class EventHandlerItem
  7. {
  8. public string eventKey;
  9. public List<EventInfo> eventInfos = new List<EventInfo>();
  10. public EventHandler action;
  11. public EventHandlerItem(string key, EventHandler handler, int time)
  12. {
  13. eventKey = key;
  14. eventInfos.Add(new EventInfo(handler, time));
  15. action += handler;
  16. }
  17. /// <summary>
  18. /// 添加事件
  19. /// </summary>
  20. /// <param name="handler">事件</param>
  21. /// <param name="time">次数</param>
  22. public void AddEventHandler(EventHandler handler, int time)
  23. {
  24. eventInfos.Add(new EventInfo(handler, time));
  25. action += handler;
  26. }
  27. /// <summary>
  28. /// 触发事件
  29. /// </summary>
  30. /// <param name="info">参数</param>
  31. public void EventHandlerTrigger(BaseEventData eventData)
  32. {
  33. action?.Invoke(eventData);
  34. for (int i = eventInfos.Count - 1; i >= 0; i--)
  35. {
  36. if (eventInfos[i].time == EventCenter.Infinity)
  37. {
  38. continue;
  39. }
  40. eventInfos[i].time--;
  41. if (eventInfos[i].time <= 0)
  42. {
  43. eventInfos.Remove(eventInfos[i]);
  44. action -= eventInfos[i].action;
  45. }
  46. }
  47. }
  48. public void RemoveEventHandler(EventHandler handler)
  49. {
  50. for (int i = eventInfos.Count - 1; i >= 0; i--)
  51. {
  52. if (eventInfos[i].action == handler)
  53. {
  54. eventInfos.Remove(eventInfos[i]);
  55. action -= handler;
  56. }
  57. }
  58. }
  59. }
  60. }