Mouse Over for GUI.Button?

Discussion in 'UnityGUI' started by Maker16, Jan 10, 2010.

  1. Maker16

    Maker16

    New Member

    Joined:
    Mar 4, 2009
    Messages:
    779
    I want a GUI.Button to vibrate when the mouse is over it. I have the animation part down. I just need the code for detecting when the mouse is over a given control.
  2. unitymatrix

    unitymatrix

    Member

    Joined:
    Dec 29, 2009
    Messages:
    94
    my mythod is using GUISkin~while which seem to be not very good~!
  3. andeeeee

    andeeeee

    Unity Technologies

    Joined:
    Jul 19, 2005
    Messages:
    8,729
    You can test if the mouse is in the control's rectangle using Rect.Contains. I don't think there is any direct way to do this via the GUI.Button call.
  4. Maker16

    Maker16

    New Member

    Joined:
    Mar 4, 2009
    Messages:
    779
    Andeeee, I considered that approach. Might still go with it. Another alternative I'm having success with is creating a game object with GUI scripting attached to it and using OnMouseOver. If I have something like 50 buttons that need to be testing for mouse over, which method is going to be more efficient in terms of resources used?
  5. sn4k3

    sn4k3

    New Member

    Joined:
    May 16, 2009
    Messages:
    36
    I found this somewhere in here.... I don't remember where so I put the code.....
    I'm using it and it's pretty easy like this...
    it looks like it updates every frame so it doesn't work very well for sounds but it should work with an animation, I think.

    I hope it helps...

    Code (csharp):
    1. var hover : String;
    2. var sound : AudioClip;
    3.  
    4. function OnGUI(){
    5.    GUI.Button (Rect (10,10,100,20), GUIContent ("Button 1", "Button 1"));
    6.    GUI.Button (Rect (110,10,100,20), GUIContent ("Button 2", "Button 2"));
    7.    hover = GUI.tooltip;
    8.    
    9.        if(hover=="Button 1")
    10.        audio.PlayOneShot(sound);
    11.    
    12.    
    13. }
    14.  
    15.  
    16. function Update () {
    17.    
    18.    
    19.    if(hover=="Button 2")
    20.    Debug.Log("Hovering Button 2");
    21. }
    ninjagamer32 likes this.
  6. andeeeee

    andeeeee

    Unity Technologies

    Joined:
    Jul 19, 2005
    Messages:
    8,729
    The tooltip idea is excellent - never thought of that before. :)
  7. Maker16

    Maker16

    New Member

    Joined:
    Mar 4, 2009
    Messages:
    779
    Actually, it is working fairly well using Rect.Contains method discussed above. I thought about the tooltip functionality, but doesn't hover have a second or so delay before it registers as a hover?
  8. sn4k3

    sn4k3

    New Member

    Joined:
    May 16, 2009
    Messages:
    36
    well.. I don't really know about the delay....

    I'm using it and it starts right after you put your mouse over it and finishes when you take it out...
    It doesn't work for sounds as I said so Imma try the Rect.contains for that...
  9. MrDude

    MrDude

    Member

    Joined:
    Sep 21, 2006
    Messages:
    1,888
    I love coding and I love arrays... Sue me, why don't ya...? :p

    How about:
    Code (csharp):
    1.  
    2. class shivverButton extends object {
    3. var area : Rect;
    4. var directionV : int = 1;
    5. var directionH : int = 1;
    6. var maxOffsetV : float = 5;
    7. var maxOffsteH : float = 5;
    8. var offsetV : float = 0.0;
    9. var offsetH: float = 0.0;
    10. var maxOffset : float = 5.0;
    11. var text  : String;
    12.  
    13. function draw(pos : Vector2) : boolean
    14. {
    15.   if (pos.x > area.x  pos.x < area.x + area.width
    16.    pos.y > area.y  pos.y < area.y + area.height)
    17.   {
    18.      offsetV = Math.Slerp(offsetV,  MaxOffsetV * directionV, Random.Range(0,0.3));
    19.      offsetH = Math.Slerp(offsetH,  MaxOffsetH * directionH, Random.Range(0,0.3));
    20.   } else
    21.   {
    22.      offsetV = Math.Slerp(MaxOffsetV * directionV, 0, Random.Range(0,0.5));
    23.      offsetH = Math.Slerp(MaxOffset H* directionH, 0, Random.Range(0,0.3));
    24.   }
    25.  
    26. if (offsetV >= MaxOffset || offsetV =< 0) directionV *= -1;
    27. if (offsetH >= MaxOffset || offsetH =< 0) directionH *= -1;
    28.  
    29.  return GUI.Button(Rect(area.x + offsetH, area.y.offsetV, area.width, area.height), text);
    30.  
    31. }
    32.  
    33. }
    34.  
    35. var MyGUIButtons : shivverButton[];
    36.  
    37. function OnGUI()
    38. {
    39. var e : Input.MousePosition;
    40. e.y = Screen.height - e.y;
    41.  
    42.      for (x = 0; x < MyGUIButtons.length; x++)
    43.      {
    44.         if ( MyGUIButtons[x].Draw(e) )
    45.         switch(x)
    46.         {
    47.           case 0: CAllOpenFunc(); break;
    48.           case 1: CAllCloseFunc(); break;
    49.           case 2: CAllStartGameFunc(); break;
    50.           case 3: CAllEndGameFunc(); break;
    51.           case 4: CAllMeFunc(); break;
    52.         }
    53.      }
    54. }
    55.  
    Okay so this is obviously untested and will undoubtedly need some cleanup but you get the basic idea... Also, this has the addd advantage of you being able to define your button size, position and text in the inspector. Graphically. Add in a ExicuteInEditor command in there and you have yourself a mini GuiX interface right there :p
  10. auguches

    auguches

    New Member

    Joined:
    Aug 21, 2007
    Messages:
    9
    I just made two conditional sentences and made it work for sounds... and other things... :D

    The variables...
    Code (csharp):
    1.  
    2.     public AudioClip soundButton;
    3.     public bool overButton = false;
    4.     public bool playedSound = false;
    5.     public string hoverButton = "";
    6.  
    On the Update function... (OnUpdate)
    Code (csharp):
    1.    
    2.                 if(overButton)
    3.         {
    4.             if(!playedSound)
    5.             {
    6.                 audio.PlayOneShot(soundButton);
    7.                 playedSound = true;
    8.             }
    9.         }
    10.         else
    11.         {
    12.             playedSound = false;
    13.         }
    14.  
    Example button... (For each button)
    Code (csharp):
    1.  
    2. if(GUI.Button(enterButtonRect, new GUIContent ("", "enterButton")))
    3. //No text or "" because is a texture button
    4.  
    On the GUI update function... (OnGUI)
    Code (csharp):
    1.  
    2.         if(hoverButton != "")
    3.             overButton = true;
    4.         else
    5.             overButton = false;
    6.  
    You can put "" on the second parameter of the GUIContent, for buttons that you don't want to play the audiclip and that's it...

    Thanks for the help! :cool:
  11. Robject

    Robject

    New Member

    Joined:
    Oct 31, 2012
    Messages:
    10
    OMG GUIContent has just saved me sooooo much hassle! I'm a total noob to this scripting malarkey and thought I was going to have to something like get different tooltips displaying based on the mouse pointer's screen position.

    I think this will solve a few other problems I've been looking into aswell so thanks very much for this sn4k3! :D
    Last edited: Oct 10, 2013
  12. DrDecipher

    DrDecipher

    Member

    Joined:
    Dec 5, 2012
    Messages:
    40