UGUI—UIHoldPressButton

作者:追风剑情 发布于:2022-11-7 15:19 分类:Unity3d

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();
    }
}

标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号