How to detect double-clicked button in editor window

Having a devil of a time coming up with a method that works on mac and windows and detects a double click only once.

Here's what I've tried:

void OnGUI()
{
    if (GUILayout.Button("DoubleClickMe"))
    {
        if (Event.current.clickCount == 2)
            Debug.Log("DoubleClick");
    }
}

This approach makes perfect sense, and works on Mac. Doesn't work on PC.

So I tried this:

void OnGUI()
{
    if (GUILayout.Button("DoubleClickMe"))
    {
    }

    if (Event.current.type == EventType.Used &&
        Event.current.clickCount == 2)
    {
        Debug.Log("DoubleClick");
    }
}

Works great on PC, but returns 2 double clicks on Mac.

Tried a few other methods, but the Mac consistently detects double clicks twice. Except for the first method, which doesn't work on PC!

If I can't get clickCount to work consistently on Mac and PC, I guess I'll try using EditorApplication.timeSinceStartup to manually detect 2 clicks close together. But I like that clickCount uses the users system double click settings - will feel right to the user...

Both Mac and PC are running 3.1.0f (54715)

Ended up doing my own test for double click. Guess I'll log a bug for the inconsistent behavior of clickCount across platforms...

The code I use in an EditorWindow:

double clickTime;
double doubleClickTime = 0.3;

void OnGUI()
{
    if (GUILayout.Button("DoubleClickMe"))
    {
        if ((EditorApplication.timeSinceStartup - clickTime) < doubleClickTime)
            DoDoubleClick();

        clickTime = EditorApplication.timeSinceStartup;
    }
}

Theoretically this could return false positives where you quickly click on 2 buttons close to each other, but I'm not worrying about that for now... You could track a button ID or something to make sure it's the same button clicked twice...

Try this:

int clickCount = 0;
float timeLapse = 0;

void OnGUI()
{
    if (GUILayout.Button("DoubleClickMe"))
    {
        if (0 == clickCount)
        {
          timeLapse = 0;
          clickCount++;
        }
        else if (1 == clickCount)
        {
          if (timeLapse > 0.5f)  // So with this 0.5f value the double click must be carried out within half a second
               timeLapse = 0;
          else
               clickCount++;
         }
        if (Event.current.clickCount == 2)
         {
            Debug.Log("DoubleClick");
            // If you dont want it to detect only once then uncomment the next line.  
            // clickCount = 0;
         }
    }

}

void Update()
{

    if (1 == clickCount)
        timeLapse += Time.deltaTime;
}

Haven't really checked the script for bugs yet but the idea is thereand it should work.

In addition to the above answers, it seems that, at least on Windows, Unity does not reset the event.clickCount when the mouse moves even a large distance, so you may need to check that too, depending on the control you are using (I'm using a SelectionGrid, so I need it):

var ev = Event.current;
var wasclick = ev.type == EventType.MouseDown;
var wasused = ev.type == EventType.Used;
selected = GUILayout.SelectionGrid(selected , ...);
if (wasclick && !wasused && ev.type == EventType.Used) {
  if ((lastClickPos - ev.mousePosition).sqrMagnitude <= 5*5 && ev.clickCount > 1)
    return true;
  lastClickPos = ev.mousePosition; // lastClickPos is class/global variable
}