using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; //对添加这个脚本的对象添加EventTrigger组件 [RequireComponent(typeof(UnityEngine.EventSystems.EventTrigger))] public class BtnPointClick : MonoBehaviour { Button btn; EventTrigger.Entry beginClick; EventTrigger.Entry pointClick; EventTrigger.Entry endClick; EventTrigger eventTrigger; void Awake() { btn = this.GetComponent<Button>(); eventTrigger = this.GetComponent<EventTrigger>(); beginClick = new EventTrigger.Entry(); pointClick = new EventTrigger.Entry(); endClick = new EventTrigger.Entry(); } void Start () { beginClick.eventID = EventTriggerType.PointerDown; pointClick.eventID = EventTriggerType.PointerClick; endClick.eventID = EventTriggerType.PointerUp; //注册事件回调 beginClick.callback = new EventTrigger.TriggerEvent(); pointClick.callback = new EventTrigger.TriggerEvent(); endClick.callback = new EventTrigger.TriggerEvent(); //添加事件监听 beginClick.callback.AddListener(BeginClickEvent); pointClick.callback.AddListener(PointClickEvent); endClick.callback.AddListener(EndClickEvent); //添加事件到委托事件 eventTrigger.triggers.Add(beginClick); eventTrigger.triggers.Add(pointClick); eventTrigger.triggers.Add(endClick); } /// <summary> /// 开始点击的事件回调 /// </summary> /// <param name="data"></param> private void BeginClickEvent(BaseEventData data) { Debug.Log("开始点击"); } /// <summary> /// 点击的事件回调 /// </summary> /// <param name="data"></param> private void PointClickEvent(BaseEventData data) { Debug.Log("点击事件"); } /// <summary> /// 鼠标抬起的事件回调 /// </summary> /// <param name="data"></param> private void EndClickEvent(BaseEventData data) { Debug.Log("鼠标抬起事件"); } }