using System; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.Serialization; /// <summary> /// 长按按钮 /// </summary> public class UIHoldPressButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler { [Tooltip("持续按下x秒")] public float holdSecond = 3; [Serializable] public class OnHoldPressedEvent : UnityEvent { } [FormerlySerializedAs("onHoldPressed")] [SerializeField] private OnHoldPressedEvent m_OnHoldPressed = new OnHoldPressedEvent(); public void OnPointerDown(PointerEventData eventData) { Invoke("OnHoldPressed", holdSecond); } public void OnPointerUp(PointerEventData eventData) { CancelInvoke(); } public void OnPointerExit(PointerEventData eventData) { CancelInvoke(); } // 触发长按事件 private void OnHoldPressed() { m_OnHoldPressed?.Invoke(); } }