123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- 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<BaseEventData> action) {
- if (obj.GetComponent<EventTrigger>() == null) {
- obj.AddComponent<EventTrigger>();
- }
- var trigger = obj.GetComponent<EventTrigger>();
- 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<EventTrigger>();
- 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<EventTrigger>();
- if (!trigger) {
- // Debug.Log(obj + "没有EventTrigger组件");
- return;
- }
- trigger.triggers.Clear();
- }
- }
|