[Solved] How to detect double tap on ui button and change interval.

Is it possible to detect double click as with guitexture ( input.GetTouch(i).tapcount==2 ) ?
Thank you in advance.

1 Like

There is a click count in the PointerEventData.

Is there an example so I can see PointerEventData in code ?

It’d be something like this. I dont have a better example.

public void OnPointerClick(PointerEventData eventData)
{
eventData.clickCount;
}

2 Likes

Could be something like this and attach the script on my image or I got it all wrong?

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class a_clickcount : MonoBehaviour, IPointerClickHandler
{
    int tap;


    public void OnPointerClick(PointerEventData eventData)
    {
        tap = eventData.clickCount;
  
        if (tap == 2)
        {
            // do something
        }

    }
}
6 Likes

should be correct.

Doesn’t work… Is there a way to implement it in the event system in drop down options (OnDoubleClick) ?

I found why it didn’t work. "IPointerClickHandler " was missing.

how can I control the click checking interval ?

You can set a timer when first tap is registered.

i meant two tap interval,to trigger double click event

You could do something like this maybe:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
    
    public class TapTest : MonoBehaviour, IPointerClickHandler
{
    int tap;
    float interval = 5f;
    bool readyForDoubleTap;
    public void OnPointerClick(PointerEventData eventData)
    {
        tap ++;

        if (tap ==1)
        {
            //do stuff

            StartCoroutine(DoubleTapInterval() );
        }

        else if (tap>1 && readyForDoubleTap)
        {
            //do stuff


            tap = 0;
            readyForDoubleTap = false;
        }
    }

    IEnumerator DoubleTapInterval()
    {   
        yield return new WaitForSeconds(interval);
        readyForDoubleTap = true;
    }
}
2 Likes

For the record, the actual click time in the Unity event system is hardcoded as 0.3 seconds (instead of 0.5 which is the default in Windows). I have no idea why they chose such a fast time and hardcoded it. The obvious-but-annoying solution is to create a new input module that is just a copy of the normal one but with that value changed.

2 Likes

hi @MattRix can you give a link to where did you get that info? because if it is like that I would like to customize the input module to work with 0.5 instead of the hardcoded 0.3, but I’m not finding any property or field to do so in my extendedStandaloneInput class which inherits from standaloneInputModule.

You can find it here (just search for clickCount): https://bitbucket.org/Unity-Technologies/ui/src/0bd08e22bc17bdf80bf7b997a4b43877ae4ee9ac/UnityEngine.UI/EventSystem/InputModules/StandaloneInputModule.cs?at=5.2&fileviewer=file-view-default

There is a much simpler and efficient approach.
Just save the last click time and compare in the next click.
Something like

float lastClick = 0f;
float interval = 0.4f;
    public void OnPointerClick(PointerEventData eventData)
    {
      if ((lastClick+interval)>Time.time)
          {//is a double click }
      else
          {//is a single click }
      lastClick = Time.time;
    }
7 Likes

Thanks for this – the logic inside is very very helpful and adaptable to any way. :slight_smile:

if i do a double click, the single click function will still occur. what can i do to not make that happen?

work for me with some changes