Get GUI Button State (onHover, onActive)

Is there a way to get “onHover” and “onActive” as a Boolean for GUI buttons? We wanted to add sound effects to a button when it is clicked and when you hover over it. Our whole game is in the GUI so there is a staggering amount of buttons, making a per-button solution unviable at this stage of development.

Are you talking about the new 4.6 Button? You should probably ask your question on the Beta Feedback forum.

No, I am talking about the current buttons. A GUI button has several states such as: normal, hover, active, onActive, onHover, ect. We are using 4.5.

EDIT: Or rather I should say a GUIStyle has those states.

For click you can put your sound effect in the if statement of the button. For hover you’ll need to check whether or not the mouse cursor is inside the rect defined for that button.

Yeah, the clicks we were able to add in just now without any major problems. And I was afraid of that…looks like I will have a lot of Rects to check.

Unless you’re doing a different hover sound for every button you can stop checking as soon as your check is true.

Still, that is a variable for every button. In any case thank you.

How is it a variable for every button? You need Rects for your buttons anyway.

Excuse my pseudocode, but wouldn’t it have to look something like this?

if (Rect.contains(x, y, x2, y2) && canClick == true)
{
if (isHovering == false)
{
isHovering = true;
soundPlay(sound);
}
}
else
{
isHovering = false;
}

Put them all in a list and then check them in a loop

while (i < buttonRects.Count)
{
    if (Rect.Contains(Input.mousePosition) && lastIndex != i)
    {
        lastIndex = i;
        soundPlay(sound);
        break;
    }
    i++;
}

A much better idea, thank you very much sir!