Hi, in the following code in the editor:
if (nameRect.Contains(Event.current.mousePosition))
{
if (Event.current.button == 2)
{
Debug.Log("pressed " + Event.current.button);
}
if (Event.current.button == 0)
{
EditorGUIUtility.ShowObjectPicker<Object>(null, true, "", 3);
Debug.Log("pressed " + Event.current.button);
}
}
It detects middle click first, but if I leave the mouse over the Rect, it then detects left click as well (which I never do, of course). I could not find any info on similar problems online, does anyone know what can be happening here? Thanks in advance!
OK, so I found a solution accidentally. I do not understand why it works the way it does, but I’d be happy if someone explained. Here’s the code that now works as intended:
Event e = Event.current;
if (e.button == 0 && e.isMouse)
{
Debug.Log("pressed " + e.button);
}
else if (e.button == 1)
{
EditorGUIUtility.ShowObjectPicker<Object>(null, true, "", 3);
Debug.Log("pressed " + e.button);
}
First, it won’t work properly if you don’t use Event e = Event.current;
Second, it won’t work properly if you don’t check e.isMouse
Finally, it won’t work properly if you first check e.button == 1 and then e.button == 0, like so:
Event e = Event.current;
if (e.button == 1 && e.isMouse)
{
Debug.Log("pressed " + e.button);
}
else if (e.button == 0)
{
EditorGUIUtility.ShowObjectPicker<Object>(null, true, "", 3);
Debug.Log("pressed " + e.button);
}
I have no idea what’s going on here…