代碼全掛在統一的地方, 如Canvas上(本篇如此作)ide
[RequireComponent(typeof(EventTrigger))]//GO 必須掛載EventTriggerui
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class CanvasJS : MonoBehaviour { private Button btnGo; private Image book; private void Start() { this.btnGo = this.transform.Find("Button").GetComponent<Button>(); this.book = this.transform.Find("Image").GetComponent<Image>(); this.AddEvent(); } private void OnDestroy() { this.RemoveEvent(); } #region 第一種方式 private void AddEvent() { this.btnGo.onClick.AddListener(this.GoHandledr); } private void RemoveEvent() { this.btnGo.onClick.RemoveListener(this.GoHandledr); } private void GoHandledr() { Debug.Log("1 , 我被觸發了!"); } #endregion }
在 Button 和 Image 上掛載 EventTriggerthis
代碼:設計
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.UI; public class EventTri : MonoBehaviour { private Button btnGo; private Image book; private void Start() { this.btnGo = this.transform.Find("Button").GetComponent<Button>(); this.book = this.transform.Find("Image").GetComponent<Image>(); this.AddEvent(this.btnGo.gameObject, this.OnBtnClick); this.AddEvent(this.book.gameObject, this.OnImgClick); } private void AddEvent(GameObject go, UnityAction<BaseEventData> listener) { EventTrigger trigger = go.GetComponent<EventTrigger>(); EventTrigger.Entry entry = new EventTrigger.Entry { eventID = EventTriggerType.PointerClick, callback = new EventTrigger.TriggerEvent() }; entry.callback.AddListener(listener); trigger.triggers.Add(entry); } private void RemoveEvent(GameObject go) { EventTrigger trigger = go.GetComponent<EventTrigger>(); trigger.triggers.Clear(); } private void OnBtnClick(BaseEventData pointData) { Debug.Log("Btn"); } private void OnImgClick(BaseEventData pointData) { Debug.Log("Img"); } }
Ⅰ, 擴展EventTriggercode
using UnityEngine; using System.Collections; using UnityEngine.EventSystems; public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger { public delegate void VoidDelegate(GameObject go); public VoidDelegate onClick; public VoidDelegate onDown; public VoidDelegate onEnter; public VoidDelegate onExit; public VoidDelegate onUp; public VoidDelegate onSelect; public VoidDelegate onUpdateSelect; static public EventTriggerListener Get(GameObject go) { EventTriggerListener listener = go.GetComponent<EventTriggerListener>(); if (listener == null) listener = go.AddComponent<EventTriggerListener>(); return listener; } public override void OnPointerClick(PointerEventData eventData) { if (onClick != null) onClick(gameObject); } public override void OnPointerDown(PointerEventData eventData) { if (onDown != null) onDown(gameObject); } public override void OnPointerEnter(PointerEventData eventData) { if (onEnter != null) onEnter(gameObject); } public override void OnPointerExit(PointerEventData eventData) { if (onExit != null) onExit(gameObject); } public override void OnPointerUp(PointerEventData eventData) { if (onUp != null) onUp(gameObject); } public override void OnSelect(BaseEventData eventData) { if (onSelect != null) onSelect(gameObject); } public override void OnUpdateSelected(BaseEventData eventData) { if (onUpdateSelect != null) onUpdateSelect(gameObject); } }
Ⅱ, Canvas 的代碼orm
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ExTrigger : MonoBehaviour { private Button btnGo; private Image book; private void Start() { this.btnGo = this.transform.Find("Button").GetComponent<Button>(); this.book = this.transform.Find("Image").GetComponent<Image>(); this.AddEvent(); } private void AddEvent() { EventTriggerListener.Get(this.btnGo.gameObject).onClick = this.OnClickHandler; EventTriggerListener.Get(this.book.gameObject).onClick = this.OnClickHandler; } private void RemoveEvent() { EventTriggerListener.Get(this.btnGo.gameObject).onClick -= this.OnClickHandler; EventTriggerListener.Get(this.book.gameObject).onClick -= this.OnClickHandler; } private void OnClickHandler(GameObject go) { if (go == this.btnGo.gameObject) { Debug.Log("Btn"); return; } if (go == this.book.gameObject) { Debug.Log("Img"); return; } } }