简体   繁体   English

如何知道Unity UI按钮是否被按下?

[英]How to know if Unity UI button is being held down?

I am using Unity 5.2 UI. 我正在使用Unity 5.2 UI。 I am working on a game for iOS. 我正在为iOS开发游戏。 I have a custom keyboard. 我有一个自定义键盘。 I want to add the functionality to the del/backspace key so that when i hold the del key for more than 2 secs, it deletes the whole word instead of a single letter, which it deletes on single clicks. 我想将功能添加到del / backspace键,以便当我按住del键超过2秒钟时,它将删除整个单词,而不是单个字母,单击一次即可删除。 How do I achieve that? 我该如何实现?

Using the UGUI event you'd create a script like the following and attach it to your button: 使用UGUI事件,您将创建一个类似于以下内容的脚本并将其附加到您的按钮上:

using UnityEngine;
using UnityEngine.EventSystems;

public class LongPress : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {

  private bool isDown;
  private float downTime;

  public void OnPointerDown(PointerEventData eventData) {
    this.isDown = true;
    this.downTime = Time.realtimeSinceStartup;
  }

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

  void Update() {
    if (!this.isDown) return;
    if (Time.realtimeSinceStartup - this.downTime > 2f) {
      print("Handle Long Tap");
      this.isDown = false;
    }
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM