Unity Community


Results 1 to 11 of 11

  1. Posts
    779

    Mouse Over for GUI.Button?

    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. Posts
    94
    my mythod is using GUISkin~while which seem to be not very good~!

  3. Unity tech writer

    Location
    Blackpool, United Kingdom
    Posts
    8,696
    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.
    I'm wired to the world... that's how I... know... everything...


  4. 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. 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:  
    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. }

  6. Unity tech writer

    Location
    Blackpool, United Kingdom
    Posts
    8,696
    The tooltip idea is excellent - never thought of that before.
    I'm wired to the world... that's how I... know... everything...


  7. 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. 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. Location
    Brits, South Africa
    Posts
    1,781
    I love coding and I love arrays... Sue me, why don't ya...? :P

    How about:
    Code:  
    1. class shivverButton extends object {
    2. var area : Rect;
    3. var directionV : int = 1;
    4. var directionH : int = 1;
    5. var maxOffsetV : float = 5;
    6. var maxOffsteH : float = 5;
    7. var offsetV : float = 0.0;
    8. var offsetH: float = 0.0;
    9. var maxOffset : float = 5.0;
    10. var text  : String;
    11.  
    12. function draw(pos : Vector2) : boolean
    13. {
    14.   if (pos.x > area.x && pos.x < area.x + area.width
    15.   && pos.y > area.y && pos.y < area.y + area.height)
    16.   {
    17.      offsetV = Math.Slerp(offsetV,  MaxOffsetV * directionV, Random.Range(0,0.3));
    18.      offsetH = Math.Slerp(offsetH,  MaxOffsetH * directionH, Random.Range(0,0.3));
    19.   } else
    20.   {
    21.      offsetV = Math.Slerp(MaxOffsetV * directionV, 0, Random.Range(0,0.5));
    22.      offsetH = Math.Slerp(MaxOffset H* directionH, 0, Random.Range(0,0.3));
    23.   }
    24.  
    25. if (offsetV >= MaxOffset || offsetV =< 0) directionV *= -1;
    26. if (offsetH >= MaxOffset || offsetH =< 0) directionH *= -1;
    27.  
    28.  return GUI.Button(Rect(area.x + offsetH, area.y.offsetV, area.width, area.height), text);
    29.  
    30. }
    31.  
    32. }
    33.  
    34. var MyGUIButtons : shivverButton[];
    35.  
    36. function OnGUI()
    37. {
    38. var e : Input.MousePosition;
    39. e.y = Screen.height - e.y;
    40.  
    41.      for (x = 0; x < MyGUIButtons.length; x++)
    42.      {
    43.         if ( MyGUIButtons[x].Draw(e) )
    44.         switch(x)
    45.         {
    46.           case 0: CAllOpenFunc(); break;
    47.           case 1: CAllCloseFunc(); break;
    48.           case 2: CAllStartGameFunc(); break;
    49.           case 3: CAllEndGameFunc(); break;
    50.           case 4: CAllMeFunc(); break;
    51.         }
    52.      }
    53. }

    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. Posts
    9

    This was very usefull...

    I just made two conditional sentences and made it work for sounds... and other things...

    The variables...
    Code:  
    1.     public AudioClip soundButton;
    2.     public bool overButton = false;
    3.     public bool playedSound = false;
    4.     public string hoverButton = "";

    On the Update function... (OnUpdate)
    Code:  
    1.                if(overButton)
    2.         {
    3.             if(!playedSound)
    4.             {
    5.                 audio.PlayOneShot(soundButton);
    6.                 playedSound = true;
    7.             }
    8.         }
    9.         else
    10.         {
    11.             playedSound = false;
    12.         }

    Example button... (For each button)
    Code:  
    1. if(GUI.Button(enterButtonRect, new GUIContent ("", "enterButton")))
    2. //No text or "" because is a texture button

    On the GUI update function... (OnGUI)
    Code:  
    1.         if(hoverButton != "")
    2.             overButton = true;
    3.         else
    4.             overButton = false;

    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!
    ---------------------------------------------
    Augusto Chesini -
    http://www.cannedbrains.com
    ---------------------------------------------


  11. 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.

    Quote Originally Posted by sn4k3 View Post
    Code:  
    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;
    I think this will solve a few other problems I've been looking into aswell so thanks very much for this sn4k3!
    Last edited by Robject; 10-10-2013 at 08:44 AM. Reason: Forgot to quote the important codey bit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •