鸟语天空
UGUI—UIHoldPressButton
post by:追风剑情 2022-11-7 15:19
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();
    }
}
评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容