Click on nothing to Deselect = Big Hassle with Unity

Hello,

I just spent the last 2 hours to try to figure out a clean way to detect a click on “nothing” (ie: no 3D colliding objects nor script created GUI Buttons under the mouse cursor during the click), so I could deselect my current selected object.

For the 3D colliding objects, I had to use a global variable updated by OnMouseEnter/OnMouseExit (which is easy clean enough), and for the GUI Buttons, I had to use the Tooltip.

But there are 2 problems with the ToolTip :

  • as far as I can tell, GUI.tooltip is only valid in the current OnGui() ; ie: if it’s set in a OnGui() of a GUI object, it will be still empty in all other GUI objects’ OnGui() ; so I had to create my own global Tooltip which must be set manually from each OnGui()
  • Tooltip is turned off when the mouse button is clicked, so I had to add 1 extra function call for this case

All in all, it’s a hell lot of stuff to just catch a mouse click that lands on nothing… And now, I still have to manually add at least 2 function calls in all my GUI objects’ OnGui() + the Tooltip for all Gui parts.

And I also have to pray that my code is bug proof with all the possible cases of interaction of Unity GUI with all of this… :expressionless:

I’d really wish it would have a simpler, faster safer way to do this. :roll:

Update :
It is actually 1 additional function call for a RepeatButton, but for a normal button, that gives this :

 if (GUI.Button(Zone, new GUIContent(MyTextureButton, "§")))
 {  // Clicked
     ForceToolTip();
 }
 else if (Input.GetMouseButton(0)  Zone.Contains(Input.mousePosition))
 { // Button Pressed but still not released
     ForceToolTip();
 }

And if like me you use virtual screen coordinates (to handle Gui scale smoothly), then you have to apply the Gui Matrix Inverse to Input.mousePosition to get the mouse position in your virtual screen.

I’m going to create my own Button() function now to handle this more cleanly…

And all of this for a click on nothing… :expressionless:

1 Like