- Home /
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)
Answer by Alex-Chouls · Nov 24, 2010 at 08:35 AM
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...
I guess you deserve credit for taking initiative on your own =)
Answer by denewbie · Nov 23, 2010 at 01:39 AM
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.
Thanks! I ended up doing something like this, but because it's running in the editor I can't use Time. Ins$$anonymous$$d I used EditorApplication.timeSinceStartup. I'll post code in an answer...
If you could find an answer on your own go for it man =). Nice of you to share it too.
Answer by Waz · Feb 15, 2011 at 10:35 AM
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
}
Your answer
Follow this Question
Related Questions
It seems like a Event.current.clickCount problem with Unity 4 1 Answer
Whats causing my objects transform gizmos to not stay with it in the editor? 2 Answers
Track passing time in EditorScript 1 Answer
Double click too fast 0 Answers
Some GameObjects not visible in android build but work fine in the editor? 0 Answers