using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; public class OBEEventManager { private static OBEEventManager mInstant; public static OBEEventManager Instant { get { if (mInstant == null) { mInstant = new OBEEventManager(); } return mInstant; } } public static void AddTriggerListener(GameObject obj,EventTriggerType eventId, UnityAction action) { if (obj.GetComponent() == null) { obj.AddComponent(); } var trigger = obj.GetComponent(); foreach (EventTrigger.Entry _entry in trigger.triggers) { if (_entry.eventID == eventId) { Debug.Log("注册了"+eventId +"事件"); break; } } EventTrigger.Entry entry = new EventTrigger.Entry(); entry.eventID = eventId; entry.callback.AddListener(action); //entry.callback.AddListener((data) => { action(data); }); trigger.triggers.Add(entry); } public static void RemoveTriggerListener(GameObject obj, EventTriggerType eventId) { var trigger = obj.GetComponent(); if (!trigger) { Debug.Log(obj+"没有EventTrigger组件"); return; } foreach (EventTrigger.Entry _entry in trigger.triggers) { if (_entry.eventID == eventId) { trigger.triggers.Remove(_entry); Debug.Log("移除"+eventId+"事件"); } } } public static void RemoveAllTriggerListener(GameObject obj) { var trigger = obj.GetComponent(); if (!trigger) { // Debug.Log(obj + "没有EventTrigger组件"); return; } trigger.triggers.Clear(); } }