OBEEventManager.cs 1.9 KB

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