I’m using the following script (found amongst these forums) to call methods on single or double click events. However, I have a problem using this on objects that also incorporate IDragHandler. I have an item in my inventory that I would like to display a tooltip when the object is clicked. However, this tooltip is showing when i drag the item too. Can anyone please tell me how to work around this?
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class ClickManager : MonoBehaviour, IPointerDownHandler
{
//You can add listeners in inspector
public UnityEvent OnSingleTap;
public UnityEvent OnDoubleTap;
float firstTapTime = 0f;
float timeBetweenTaps = 0.2f; // time between taps to be resolved in double tap
bool doubleTapInitialized;
public void OnPointerDown(PointerEventData eventData)
{
// Invoke single tap after max time between taps
Invoke("SingleTap", timeBetweenTaps);
if (!doubleTapInitialized)
{
//Init double tapping
doubleTapInitialized = true;
firstTapTime = Time.time;
}
else if (Time.time - firstTapTime < timeBetweenTaps)
{
//Here we have tapped second time before "single tap" has been invoked
CancelInvoke("SingleTap"); //Cancel "single tap" invoking
DoubleTap();
}
}
void SingleTap()
{
doubleTapInitialized = false; //De-init double tap
//Fire OnSingleTap event for all eventual subscribers
if(OnSingleTap != null)
{
OnSingleTap.Invoke();
}
}
void DoubleTap()
{
doubleTapInitialized = false;
if(OnDoubleTap != null)
{
OnDoubleTap.Invoke();
}
}
}