Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  6. Unity 2017.2 beta is now available for download.
    Dismiss Notice

Mouse Over for GUI.Button?

Discussion in 'Extensions & OnGUI' started by Maker16, Jan 10, 2010.

  1. Maker16

    Maker16

    Joined:
    Mar 4, 2009
    Posts:
    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

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

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    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

    Joined:
    Mar 4, 2009
    Posts:
    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

    Joined:
    May 16, 2009
    Posts:
    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. }
     
    RodrigoSeVeN and ninjagamer32 like this.
  6. andeeeee

    andeeeee

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

    Maker16

    Joined:
    Mar 4, 2009
    Posts:
    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

    Joined:
    May 16, 2009
    Posts:
    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

    Joined:
    Sep 21, 2006
    Posts:
    2,336
    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

    Joined:
    Aug 21, 2007
    Posts:
    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

    Joined:
    Oct 31, 2012
    Posts:
    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

    Joined:
    Dec 5, 2012
    Posts:
    52
  13. milu2015

    milu2015

    Joined:
    Jan 21, 2015
    Posts:
    1
    who can help me or say to do:
    I spend my pointer for button1, which has a text, then the text button1 button2 can instruct someone to appear?

    is that I work with over event (onmouseover), but not how to create the code :(