Unity | 工具类-按钮长按

 1.继承Button,利用OnPointerDown和OnPointerUp来实现长按监听。

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


public class ButtonPress : Button
{
    public bool isPointerDown = false;
    private float timePressStarted;
    private float durationThreshold = 0.2f; // 长按的阈值时间

    public UnityEvent OnPress = new UnityEvent(); // 长按事件
    public UnityEvent OnPressEnd = new UnityEvent(); // 长按结束事件

    private void Update()
    {
        if (isPointerDown)
        {
            if (Time.time - timePressStarted > durationThreshold) // 检查是否超过阈值
            {
                OnPress.Invoke(); 
            }
        }
    }

    public override void OnPointerDown(PointerEventData eventData)
    {
        base.OnPointerDown(eventData); // 调用基类的方法
        if (eventData.button != PointerEventData.InputButton.Left)
            return;

        timePressStarted = Time.time;
        isPointerDown = true;
    }

    public override void OnPointerUp(PointerEventData eventData)
    {
        base.OnPointerUp(eventData); // 调用基类的方法
        isPointerDown = false;
        OnPressEnd.Invoke();
    }
}

2.左右两个按钮添加ButtonPress组件,实现按钮长按时控制Cube左右移动

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

public class TestMyButton : MonoBehaviour
{
    public GameObject uiButtonCanvas;
    public GameObject cube;
    bool isLeft = false;
    bool isRight = false;
    // Start is called before the first frame update
    void Start()
    {

        ButtonPress leftButton = uiButtonCanvas.transform.Find("leftButton").GetComponent<ButtonPress>();
        leftButton.OnPress.AddListener(() =>
        {
            isLeft = true;

        });
        leftButton.OnPressEnd.AddListener(() =>
        {
            isLeft = false;
        });
        ButtonPress rightButton = uiButtonCanvas.transform.Find("rightButton").GetComponent<ButtonPress>();
        rightButton.OnPress.AddListener(() =>
        {
            isRight = true;
        });
        rightButton.OnPressEnd.AddListener(() =>
        {
            isRight = false;
        });
    }

    // Update is called once per frame
    void Update()
    {
        if (isLeft)
        {
            cube.transform.localPosition += Vector3.left * 0.1f;
        }
        else if (isRight)
        {
            cube.transform.localPosition += Vector3.right * 0.1f;

        }
        else
        {
            cube.transform.localPosition += Vector3.forward * 0.1f;

        }
    }
}

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您可以通过以下步骤实现Unity按钮播放视频松开按钮: 1. 创建一个按钮并添加OnClick事件。 2. 在OnClick事件,使用InvokeRepeating()函数来定期调用一个方法,该方法将在按钮期间一直执行。 3. 在该方法,检测按钮是否已松开。如果按钮已松开,则停止InvokeRepeating()。 4. 在该方法,检测按钮是否已按下。如果按钮已按下,则播放视频。 下面是示例代码: ```csharp public class ButtonController : MonoBehaviour { public GameObject videoPlayer; private bool isPressed = false; public void OnButtonDown() { isPressed = true; InvokeRepeating("CheckButton", 0, 0.1f); } public void OnButtonUp() { isPressed = false; CancelInvoke("CheckButton"); } private void CheckButton() { if (isPressed) { // 播放视频 videoPlayer.SetActive(true); } else { // 停止播放视频 videoPlayer.SetActive(false); CancelInvoke("CheckButton"); } } } ``` 在上面的示例代码,我们创建了一个名为ButtonController的脚本,并将其添加到按钮上。该脚本包含了两个方法:OnButtonDown()和OnButtonUp(),分别在按钮按下和松开时调用。我们还添加了一个名为videoPlayer的游戏对象,它将在按钮按期间用于播放视频。 在OnButtonDown()方法,我们将isPressed设置为true,并使用InvokeRepeating()函数定期调用CheckButton()方法。在CheckButton()方法,我们检测按钮是否已按下或已松开,并根据需要播放或停止视频。最后,在OnButtonUp()方法,我们将isPressed设置为false,并取消InvokeRepeating()。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烫青菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值