jibrangillani11
jibrangillani11

Reputation: 61

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

I am using Unity 5.2 UI. I am working on a game for 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. How do I achieve that?

Upvotes: 0

Views: 6235

Answers (1)

slumtrimpet
slumtrimpet

Reputation: 3277

Using the UGUI event you'd create a script like the following and attach it to your button:

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;
    }
  }

}

Upvotes: 1

Related Questions