1

Below code use to implement the backspace button functionality which runs under a GUI button OnClick.

 public void BackSpace(InputField userField) {
        string textEnter = userField.text;
        string tempString = textEnter.Substring(0, textEnter.Length - 1);
        userField.text = tempString;
    }

It working fine and remove input text one at a time single button click. The problem is that I want to run this functions continuously as user press continuously the GUI button Just like your keyboard backspace button but sadly this is working only once as onClick work only once. Is there any event or trick available to detect continuous touch on GUI in order to run my desired code?

Fattie
  • 27,874
  • 70
  • 431
  • 719
Muhammad Faizan Khan
  • 10,013
  • 18
  • 97
  • 186
  • For any beginners googling here, here's an essay explaining how to access the "other" calls from something like a UI.Button http://stackoverflow.com/a/36046495/294884 – Fattie Mar 29 '16 at 13:40
  • you should absolutely be using UnityEvent here. Make a script called BetterButton with a UnityEvent, Tap. To implement those two concepts use Fafase's code below. Then attach BetterButton to any button to make it into that type of button. Here is an explanation of "making your own" UnityEvent for any beginners googling along to here http://stackoverflow.com/questions/36244660/simple-event-system-in-unity/36249404#36249404 – Fattie Mar 29 '16 at 13:46

2 Answers2

2

You may want to look into the Selectable event system. You can implement the OnPonterDown and OnPointerUp so that the first set the triggering and the second stops it:

using UnityEngine.EventSystems;
public class Test : MonoBehaviour, IPointerDownHandler, IPointerUpHandler {
    [SerializeField]private InputField userField;
    #region IPointerUpHandler implementation
    private bool running =false;
    public void OnPointerUp (PointerEventData eventData)
    {
        running = false;
        if(timer < holdTime){ Tap(); }
        timer = 0.0f;
    }

    #endregion

    #region IPointerDownHandler implementation

    public void OnPointerDown (PointerEventData eventData)
    {
        running = true;
    }

    #endregion
    float timer = 0.0f;
    float holdTime = 0.25f;
    // Update is called once per frame
    void FixedUpdate () 
    {
        if(running){ 
           timer += Time.deltaTime;
           if(timer > holdTime)
                BackSpace();}
    }
    private void BackSpace() {
        string textEnter = userField.text;
        string tempString = textEnter.Substring(0, textEnter.Length - 1);
        userField.text = tempString;
    }
}

The fixed update makes it consistent over different devices. You could set your own update system to control the frequency.

Everts
  • 10,408
  • 2
  • 34
  • 45
1

Just to save anyone typing, here's exactly how you do it:

Put this script on the button

You will in fact have some other script ( SomeScript ) which has a toggle in it.

Copy and paste!

using UnityEngine;
using UnityEngine.EventSystems;

public class HoldableButton: MonoBehaviour, IPointerDownHandler, IPointerUpHandler {

    bool _down;
    public SomeScript someScript;

    public void OnPointerDown(PointerEventData eventData) {
        _down = true;
    }

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

    void Update() {

        someScript.yourToggle = _down;
    }
}
Fattie
  • 27,874
  • 70
  • 431
  • 719