Unity 单击、双击、长按事件处理

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class ButtonOnClick : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{

    // 按压的持续时间
    float pressDurationTime = 1;
    // 按压的响应次数
    bool responseOnceByPress = false;
    // 双击的间隔时间
    float doubleClickIntervalTime = 0.5f;
    // 拖动的间隔时间
    float dragIntervalTime = 0.2f;
    // 拖动的鼠标间隔距离
    float dragIntervalPos = 0.01f;

    /// <summary>
    /// 双击事件
    /// </summary>
    public UnityEvent onDoubleClick;
    /// <summary>
    /// 按压事件
    /// </summary>
    public UnityEvent onPress;
    /// <summary>
    /// 点击事件
    /// </summary>
    public UnityEvent onClick;
    /// <summary>
    /// 拖动事件
    /// </summary>
    public UnityEvent onDrag;

    private bool isDown = false;
    private bool isPress = false;
    private bool isDrag = false;
    private float downTime = 0;

    private float clickIntervalTime = 0;
    private int clickTimes = 0;

    private Vector3 mousePosLast = Vector3.zero;//点击后的拖动位置

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("鼠标左键点击");
        }

        if (isDown)
        {
            if (responseOnceByPress && isPress)
            {
                return;
            }
            downTime += Time.deltaTime;
            isDrag = Vector3.Distance(Input.mousePosition, mousePosLast) > dragIntervalPos;
            if (downTime > pressDurationTime && !isDrag)
            {
                isPress = true;
                onPress.Invoke();
            }
            if (downTime > dragIntervalTime && isDrag)
            {
                onDrag.Invoke();
            }
        }
        if (clickTimes >= 1)
        {
            clickIntervalTime += Time.deltaTime;
            if (clickIntervalTime >= doubleClickIntervalTime)
            {
                if (clickTimes >= 2)
                {
                    onDoubleClick.Invoke();
                }
                else
                {
                    onClick.Invoke();
                }
                clickTimes = 0;
                clickIntervalTime = 0;
            }
        }
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (!isPress)
            clickTimes += 1;
        else
            isPress = false;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        isDown = true;
        downTime = 0;
        mousePosLast = Input.mousePosition;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        isDown = false;
        isPress = false;
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        isDown = false;
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值