Search Unity

How to detect long press or double tap or swipe on UI button (Unity 4.6)?

Discussion in 'UGUI & TextMesh Pro' started by axcs, Jan 12, 2015.

  1. axcs

    axcs

    Joined:
    Jan 6, 2015
    Posts:
    17
    I am already a few days trying to figure out how can I detect if a few buttons existed long press touch, the new UI interface Unity 4.6 in C #.

    Sorry for my bad english, and if I can help or explain would be grateful. I want to have multiple buttons, and each of whether the user has exercised long press touch.
     
  2. Glorion13

    Glorion13

    Joined:
    Nov 19, 2012
    Posts:
    8
    Hi axcs,

    Here is one easy solution for detecting long press.

    1) First of all, I would add an Event Trigger component to each one of my buttons.
    2) On the Event Trigger component of each button, I would add an event type for Pointer Down and an event type for Pointer Up.
    3) I would then code my Pointer Down and Pointer Up to create a 'long press'.

    The function triggered by Pointer Down will be something like this:
    public void PointerDown()
    {
    StartCoroutine(LongPressResult());
    }

    The function triggered by Pointer Up will be something like this:
    public void PointerUp()
    {
    StopCoroutine(LongPressResult());
    }

    Finally, you need to code the following which is what will happen on a long press, as well as defining how long is a 'long press'.

    public float LongPressDuration; // you should set this equal to the duration you want for a long press, e.g. 2 seconds.
    public IEnumerator LongPressResult()
    {
    yield return new WaitForSeconds(LongPressDuration);
    // Here type the code for what you want to happen at the end of the long press
    }

    I hope this helps! :)
     
    Daoqi likes this.