Search Unity

Get GUI Button State (onHover, onActive)

Discussion in 'Scripting' started by Afropenguinn, Aug 22, 2014.

  1. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    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.
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Are you talking about the new 4.6 Button? You should probably ask your question on the Beta Feedback forum.
     
  3. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    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.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    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.
     
  5. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    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.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Unless you're doing a different hover sound for every button you can stop checking as soon as your check is true.
     
  7. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    Still, that is a variable for every button. In any case thank you.
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    How is it a variable for every button? You need Rects for your buttons anyway.
     
  9. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    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;
    }
     
  10. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Put them all in a list and then check them in a loop

    Code (csharp):
    1.  
    2. while (i < buttonRects.Count)
    3. {
    4.     if (Rect.Contains(Input.mousePosition) && lastIndex != i)
    5.     {
    6.         lastIndex = i;
    7.         soundPlay(sound);
    8.         break;
    9.     }
    10.     i++;
    11. }
    12.  
     
  11. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    A much better idea, thank you very much sir!