gpt4 book ai didi

c# - Unity 防止点击按钮

转载 作者:行者123 更新时间:2023-12-05 07:45:48 26 4
gpt4 key购买 nike

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

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 中会调用点按,但在移动设备上,它可能会在鼠标松开时触发...暂时这会阻止事件传播,

关于c# - Unity 防止点击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41294784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com