private void AddTriggersListener(GameObject obj, EventTriggerType eventID, UnityAction<BaseEventData> action) { EventTrigger trigger = obj.GetComponent<EventTrigger>(); if (trigger == null) { trigger = obj.AddComponent<EventTrigger>(); } if (trigger.triggers.Count == 0) { trigger.triggers = new List<EventTrigger.Entry>(); } UnityAction<BaseEventData> callback = new UnityAction<BaseEventData>(action); EventTrigger.Entry entry = new EventTrigger.Entry(); entry.eventID = eventID; entry.callback.AddListener(callback); trigger.triggers.Add(entry); }
上面这个函数是重点中的重点
用法如下
public GameObject showImg; // Use this for initialization void Start () { AddTriggersListener(showImg.gameObject,EventTriggerType.PointerClick,resetFunc); } private void resetFunc(BaseEventData data) { showImg.SetActive(false); }