How do I know if GUI is selected?

Is there a way to find out if the GUI.Button texture has changed to hover? Or anyway to find out if the mouse is over the button.

Hello,

Unity’s GUI is very crappy and should only get used for prototyping (at least until 4.2 is released). On that note, there is no built in way of finding out the buttons state like there is (and should be) in any modern GUI modules.

There is however, a workaround:
Instead of finding out the buttons state, you calculate for yourself if the mouse is over the button:

//finding wether mouse is in rectangle
function InRect(rectangle:Rect) {
    return Input.mousePosition.x > rectangle.x &&
        Input.mousePosition.x < rectangle.x + rectangle.width &&
        Input.mousePosition.y > rectangle.y &&
        Input.mousePosition.y < rectangle.y = rectangle.height;
}

//using GUI
var rect:Rect = Rect(0, 0, 100, 200);
function OnGUI() {
    GUI.Button(rect, /*content*/);
    if (InRect(rect)) {
        //we are over the button
    }
}

//using GUILayout
function OnGUI() {
    GUILayout.Button(/*content*/)
    if (InRect(GUILayoutUtility.GetLastRect())) {
        //we are over the button
    }
}

Note that this code is untested, so there might be some bugs :slight_smile:

Hope this helps,
Benproductions1