Unity长按点击功能

Unity监听长按点击与单点方法

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class LongPressOrClickEventTrigger : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler, IPointerClickHandler
{
    public float durationThreshold = 1.0f;
    public UnityEvent onLongPress = new UnityEvent();
    public UnityEvent onClick = new UnityEvent();
    private bool isPointerDown = false;
    private bool longPressTriggered = false;
    private float timePressStarted;
    private void Update()
    {
        if (isPointerDown && !longPressTriggered)
        {
            if (Time.time - timePressStarted > durationThreshold)
            {
                longPressTriggered = true;
                onLongPress.Invoke();
            }
        }
    }
    public void OnPointerDown(PointerEventData eventData)
    {
        timePressStarted = Time.time;
        isPointerDown = true;
        longPressTriggered = false;
    }
    public void OnPointerUp(PointerEventData eventData)
    {
        isPointerDown = false;
    }
    public void OnPointerExit(PointerEventData eventData)
    {
        isPointerDown = false;
    }
    public void OnPointerClick(PointerEventData eventData)
    {
        if (!longPressTriggered)
        {
            onClick.Invoke();
        }
    }
}

同样的长按与单点监听,并监听按钮的抬起与按下

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class LongPressButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
    
    [Tooltip("长按多少秒触发时间")]
    public float intervalTime = 2f;

    [Tooltip("长按每次多久触发一次事件")]
    public float trrigerIntervalTime = 0.5f;

    [Tooltip("长按每次最短多久触发一次事件")]
    public float trrigerIntervalMinTime = 0.1f;

    [Tooltip("长按触发事件间隔每次减少的时间")]
    public float trrigerReduceTime = 0.1f;

    [Tooltip("是否可以触发")]
    public bool interactable = true;

    #region 事件
    [Tooltip("长按中触发什么事件")]
    public UnityEvent onPressing;
    [Tooltip("长按到触发间隔后只触发一次事件")]
    public UnityEvent onPressBegin;
    [Tooltip("长按开始触发的事件")]
    public UnityEvent onStartPress;
    [Tooltip("退出长按时触发的事件")]
    public UnityEvent onEndPress;
    #endregion

    [HideInInspector]
    public bool isPressing { get; private set; }

    private bool m_IsPressingBegin = false;
    private bool m_IsStartPress = false;
    private float m_CountTime;
    private float m_TriggerCountTime;
    private float m_TriggerReduceCountTime;
    
    private void Update()
    {
        if (interactable)
        {
            if (m_IsStartPress)
            {
                m_CountTime = intervalTime;
                m_TriggerReduceCountTime = trrigerIntervalTime;
                m_IsStartPress = false;
                if (onStartPress != null)
                    onStartPress.Invoke();
            }
            if (isPressing)
            {
                m_CountTime -= Time.deltaTime;
                if (m_CountTime <= 0)
                {
                    if (m_TriggerCountTime > 0)
                    {
                        m_TriggerCountTime -= Time.deltaTime;
                    }
                    else
                    {
                        m_TriggerCountTime = m_TriggerReduceCountTime;
                        if (m_TriggerReduceCountTime > trrigerIntervalMinTime)
                            m_TriggerReduceCountTime -= trrigerReduceTime;
                        if (!m_IsPressingBegin)
                        {
                            if (onPressBegin != null)
                                onPressBegin.Invoke();
                            m_IsPressingBegin = true;
                        }
                        if (onPressing != null)
                            onPressing.Invoke();
                    }
                }
            }
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        m_IsStartPress = true;
        isPressing = true;
        m_IsPressingBegin = false;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        m_IsStartPress = false;
        isPressing = false;
        if (onEndPress != null)
            onEndPress.Invoke();
    }
}

你可能感兴趣的:(Unity工具)