Unity 防止点击按钮

Unity Prevent Click On Button

我正在为长按按钮创建脚本。长按按钮有效,唯一的问题是如果长按按钮触发我想阻止点击按钮。

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

public class LongPressButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
    [Tooltip("How long must pointer be down on this object to trigger a long press")]
    public float durationThreshold = 1.0f;

    public UnityEvent onLongPress = 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;
    }
}

按钮脚本是黑框的,它似乎内置在 unity 引擎中。

我知道有一些方法可以防止事件传播到 3d 世界,但是有没有一种方法可以防止在按钮脚本上传播?

我已经弄清楚了,由于事件系统的 Unity 限制,您将需要两个脚本来执行此操作。首先长按按钮脚本:

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

public class LongPressButton : UIBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
    [Tooltip("How long must pointer be down on this object to trigger a long press")]
    public float durationThreshold = 1.0f;

    public UnityEvent onLongPress = new UnityEvent(); 

    private bool isPointerDown = false;
    private bool longPressTriggered = false;
    private float timePressStarted;

    public bool WasLongPressTriggered
    {
        get;
        protected set;
    }

    private void Update()
    {
        if (isPointerDown && false == longPressTriggered)
        {
            if (Time.time - timePressStarted > durationThreshold)
            {
                longPressTriggered = true;
                WasLongPressTriggered = true;
                onLongPress.Invoke();
            }
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        timePressStarted = Time.time;
        isPointerDown = true;
        longPressTriggered = false;
        WasLongPressTriggered = false;
    }

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


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

那么您将需要一个新的按钮脚本来覆盖旧的基本按钮:

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

public class Button2 : Button
{
    public bool PreventPropagationOnLongPress;

    public override void OnPointerClick(PointerEventData eventData)
    {
        var longPress = GetComponent<LongPressButton>();
        if (longPress == null)
        {
            base.OnPointerClick(eventData);
        }
        if(false == longPress.WasLongPressTriggered)
        {
            base.OnPointerClick(eventData);
        }
    }
}

这在编辑器中有效,但尚未在移动设备上进行全面测试,可能在 unity edidor 中会调用点按,但在移动设备上,它可能会在鼠标松开时触发...暂时如此将阻止事件传播,