【问题标题】:Unity3D: How to detect when a button is being held down and released [duplicate]Unity3D:如何检测按钮何时被按住和释放[重复]
【发布时间】:2019-04-01 05:26:59
【问题描述】:

我有一个 UI 按钮。我想在用户按下按钮时显示文本,并在用户释放按钮时隐藏文本。

我该怎么做?

【问题讨论】:

  • 要么使用此处给出的答案How to detect click/touch events on UI and GameObjects 中给出的脚本,要么使用EventTrigger 并在OnPointerDownOnPointerUp 事件中调用您想要的函数。
  • 为了在按下按钮时调用函数,请使用您在前面的事件中设置为true/false 的布尔值,并检查Update 中的布尔值在调用你想要的函数之前调用函数。

标签: user-interface unity3d button


【解决方案1】:

this anwser 基本上没问题,但有一个很大的缺点:你不能在 Inspector 中添加额外的字段,因为 Button 已经有一个内置的 EditorScript,它会覆盖默认的 Inspector。


我会将其实现为实现IPointerDownHandlerIPointerUpHandler 的完全附加组件(也可能IPointerExitHandler 在按住鼠标/指针退出按钮时也重置)。

按钮保持按下状态时,我会使用Coroutine

一般我会使用UnityEvents:

[RequireComponent(typeof(Button))]
public class PointerDownUpHandler : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
{
    public UnityEvent onPointerDown;
    public UnityEvent onPointerUp;

    // gets invoked every frame while pointer is down
    public UnityEvent whilePointerPressed;

    private Button _button;

    private void Awake()
    {
        _button = GetComponent<Button>();
    }

    private IEnumerator WhilePressed()
    {
        // this looks strange but is okey in a Coroutine
        // as long as you yield somewhere
        while(true)
        {
             whilePointerPressed?.Invoke();
             yield return null;
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        // ignore if button not interactable
        if(!_button.interactable) return;

        // just to be sure kill all current routines
        // (although there should be none)
        StopAllCoroutines();
        StartCoroutine(WhilePressed);

        onPointerDown?.Invoke();
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        StopAllCoroutines();
        onPointerUp?.Invoke();
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        StopAllCoroutines();
        onPointerUp?.Invoke();
    }

    // Afaik needed so Pointer exit works .. doing nothing further
    public void OnPointerEnter(PointerEventData eventData) { }
}

您可以引用onPointerDownonPointerUpwhilePointerPressed 中的任何回调,就像使用ButtononClick 事件一样。

【讨论】:

    【解决方案2】:

    您必须通过扩展 Button 类并覆盖方法 OnPoiterDown 和 OnPointerUp 来创建自己的自定义按钮。 将 MyButton 组件而不是 Button 附加到您的游戏对象

    using UnityEngine;
    using UnityEngine.EventSystems;
    using UnityEngine.UI;
    
    public class MyButton : Button
    {
    
        public override void OnPointerDown(PointerEventData eventData)
        {
            base.OnPointerDown(eventData);
            Debug.Log("Down");
            //show text
        }
    
        public override void OnPointerUp(PointerEventData eventData)
        {
            base.OnPointerUp(eventData);
            Debug.Log("Up");
            //hide text
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-14
      • 1970-01-01
      • 2012-07-27
      • 2012-10-10
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多