EventManagerPatch.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. public class EventManagerPatch
  7. {
  8. private static EventManager mInstant;
  9. public static EventManager Instant
  10. {
  11. get
  12. {
  13. if (mInstant == null)
  14. {
  15. mInstant = new EventManager();
  16. }
  17. return mInstant;
  18. }
  19. }
  20. public static void AddTriggerListener(GameObject obj, EventTriggerType eventId, UnityAction<BaseEventData> action)
  21. {
  22. if (obj.GetComponent<EventTrigger>() == null)
  23. {
  24. obj.AddComponent<EventTrigger>();
  25. }
  26. var trigger = obj.GetComponent<EventTrigger>();
  27. foreach (EventTrigger.Entry _entry in trigger.triggers)
  28. {
  29. if (_entry.eventID == eventId)
  30. {
  31. Debug.Log("注册了" + eventId + "事件");
  32. break;
  33. }
  34. }
  35. EventTrigger.Entry entry = new EventTrigger.Entry();
  36. entry.eventID = eventId;
  37. entry.callback.AddListener(action);
  38. //entry.callback.AddListener((data) => { action(data); });
  39. trigger.triggers.Add(entry);
  40. }
  41. public static void RemoveTriggerListener(GameObject obj, EventTriggerType eventId)
  42. {
  43. var trigger = obj.GetComponent<EventTrigger>();
  44. if (!trigger)
  45. {
  46. Debug.Log(obj + "没有EventTrigger组件");
  47. return;
  48. }
  49. foreach (EventTrigger.Entry _entry in trigger.triggers)
  50. {
  51. if (_entry.eventID == eventId)
  52. {
  53. trigger.triggers.Remove(_entry);
  54. Debug.Log("移除" + eventId + "事件");
  55. }
  56. }
  57. }
  58. public static void RemoveAllTriggerListener(GameObject obj)
  59. {
  60. var trigger = obj.GetComponent<EventTrigger>();
  61. if (!trigger)
  62. {
  63. // Debug.Log(obj + "没有EventTrigger组件");
  64. return;
  65. }
  66. trigger.triggers.Clear();
  67. }
  68. }