I have a different approach which I think is slightly closer to standards. Hope it helps!
This doesnât use the incremental click because (as mentioned) that doesnât stick to timing standards, and ALSO, itâs possible to get a false positive double click because itâs consecutive clicks anywhere, not just on the object being clicked. This means that your first click could be on object A, and your second click on object B, and if itâs fast enough, itâd still count as a double click. Itâs an edgecase, admittedly, but that doesnât happen as standard on say, windows.
The other thing to note here is that double clicks donât occur on the âclickâ event (which is when you press then release on the same object). The occur on the second consecutive pointer down event within 500ms of the last pointer down event.
So basically, what I do here is record the object that is clicked on by the click event (by using the pointerCurrentRaycast.gameObject). If I click down again, I check if itâs the same object being clicked as last time. If it is, and the time since last click (in unscaled time!!) is less than a half a second, I can trigger the double click.
The âLASTPOINTERDOWNOBJECTâ is static, and maybe you want to record one of these for each mouse clickâs
InputButton, so that left, then right click doesnât make a double click.
Might get around to codifying this into an IPointerDoubleClick event that you can Execute up the hierarchy, but thatâll mean extending out an event system first.
using UnityEngine;
using UnityEngine.EventSystems;
public class UICard : MonoBehaviour, IPointerDownHandler
{
const float doubleClickTime = 0.5f;//Wish I could trivially plum this into the system's doubleclick time.
private static GameObject LASTPOINTERDOWNOBJECT = null;//Need to record the last click event's object, so that double clicking doesn't occur when skipping across objects (avoid accidental double click). I tried looking through PointerEventData, but none of the values I expected came back with a record of the previously clicked/hovered object.
public void OnPointerDown(PointerEventData eventData)
{
float timeSinceLastClick = Time.unscaledTime - eventData.clickTime;//Note use of unscaled time. I've tested with Time.timeScale = 0.0f; and this still works.
bool bSameObjectClicked = LASTPOINTERDOWNOBJECT == eventData.pointerCurrentRaycast.gameObject;
if ( timeSinceLastClick < doubleClickTime && bSameObjectClicked)
{
Debug.Log("DoubleClick pressed!");
}
LASTPOINTERDOWNOBJECT = eventData.pointerCurrentRaycast.gameObject;
}
}