unity——Button组件单击双击长按功能

1.实现单击、双击、长按功能

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ButtonControl_Click_Press_Double : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
    public float pressDurationTime = 1;
    public bool responseOnceByPress = false;
    public float doubleClickIntervalTime = 0.5f;

    public UnityEvent onDoubleClick;
    public UnityEvent onPress;
    public UnityEvent onClick;

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

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

    void Update()
    {
        if (isDown)
        {
            if (responseOnceByPress && isPress)
            {
                return;
            }
            downTime += Time.deltaTime;
            if (downTime > pressDurationTime)
            {
                isPress = true;
                onPress.Invoke();
            }
        }
        if (clickTimes >= 1)
        {
            clickIntervalTime += Time.deltaTime;
            if (clickIntervalTime >= doubleClickIntervalTime)
            {
                if (clickTimes >= 2)
                {
                    onDoubleClick.Invoke();
                }
                else
                {
                    onClick.Invoke();
                }
                clickTimes = 0;
                clickIntervalTime = 0;
            }
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        isDown = true;
        downTime = 0;
    }

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

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

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

2.使用

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ButtonsControl_1 : MonoBehaviour
{
    ButtonControl_Click_Press_Double btn;

    void Start()
    {
        btn = GetComponent<ButtonControl_Click_Press_Double>();
        btn.onClick.AddListener(Click);
        btn.onPress.AddListener(Press);
        btn.onDoubleClick.AddListener(DoubleClick);
    }

    void Click()
    {
        Debug.Log("click");
    }

    void Press()
    {
        Debug.Log("press");
    }

    void DoubleClick()
    {
        Debug.Log("double click");
    }
}

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-12 17:46:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-12 17:46:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-12 17:46:01       87 阅读
  4. Python语言-面向对象

    2024-04-12 17:46:01       96 阅读

热门阅读

  1. TCP/IP协议介绍

    2024-04-12 17:46:01       48 阅读
  2. 前端编译时和运行时(简单易懂快速理解)

    2024-04-12 17:46:01       43 阅读
  3. arm 的system IP有哪些?

    2024-04-12 17:46:01       125 阅读
  4. 碳交易机制下考虑需求响应的优化运行-MATLAB复现

    2024-04-12 17:46:01       36 阅读
  5. Linux命令学习—linux 的文件系统

    2024-04-12 17:46:01       47 阅读
  6. [MySQL] 慢查询

    2024-04-12 17:46:01       50 阅读
  7. Python格式化输出的三种常用方式

    2024-04-12 17:46:01       38 阅读
  8. react中useMemo 钩子函数的使用

    2024-04-12 17:46:01       42 阅读
  9. 滑动窗口和螺旋矩阵(二十天)

    2024-04-12 17:46:01       107 阅读
  10. 人工智能在哪些行业赋能

    2024-04-12 17:46:01       93 阅读
  11. iperf3使用记录

    2024-04-12 17:46:01       41 阅读
  12. C#设计简单的WinForms多窗口应用程序[示例]

    2024-04-12 17:46:01       32 阅读
  13. Linux apt 命令

    2024-04-12 17:46:01       35 阅读
  14. Python_day26

    2024-04-12 17:46:01       41 阅读
  15. Netty框架介绍并编写WebSocket服务端与客户端

    2024-04-12 17:46:01       33 阅读