The current GUI code is wayyyyyyyy over simplified approach..
GUILayout.Button("I'm a button!");
Ok so now when is the mouse over the button?
Some ridiculous solutions I found:
You can check what tooltip is displayed
And what if I have 2 buttons with same tooltip?
The button already knows when a mouse is over it - it changes its border, no need to add redundant code.
So how do I know when the mouse is over a button without workarounds, and where is that hidden real OOP gui package?
Yes, there is one. the example for this is the textarea - it remembers where the cursor is without you passing it to it and getting the new position in return.
Well, there really is not a OOP GUI package for UnityGui, however, underneath, the GUI system uses the old procedural approach to OOP, which is why it looks like it is OOP. Anyway, to answer your question, there is no way to get weather the mouse is over a element without saving the rect and seeing if the mouse is within it. UnityGUI provides much less programmatic customization than other (non-Unity) GUI systems.
Try GUIX, a plugin developed by Ennanuzus Interactive. It is an OOP layer over the existing Immediate GUI system. It is not free though; do try out the demo and see if you like it.
If you got a little patience, you can make classes (don't derive from MonoBehaviour) for each gui type and expose useful variables such as Rect, GUIContent, GUIStyle and other related variables. That way you can build an OO GUI from an immediate GUI. I used it many times. Tip: Make your classes Serializable and expose them in any script that uses them. The inspector will allow you to make simple changes and you can easily embed a mouse-over system in those classes.
I think sample code easily carry the intention over:
public class GUIButton
public Rect position = new Rect(0, 0, 200, 100);
public GUIContent content = GUIContent.none;
public string style = "button";
public bool OnGUI()
return GUI.Button(position, content, style);
public bool IsMouseOver()
// Dunno if you need to convert the position to GUI coordinates
// using GUIUtility.ScreenToGUIPoint...
See also: GUIUtility.ScreenToGUIPoint.
You should check out eDriven.Gui.
It’s a full blown OOP, event-driven, asynchronous GUI/RIA solution for Unity3d: http://edrivengui.com/