Search Unity

Figuring out which button the mouse is hovering over

Discussion in 'Scripting' started by Luke-Houlihan, Mar 24, 2008.

  1. Luke-Houlihan

    Luke-Houlihan

    Joined:
    Jun 26, 2007
    Posts:
    303
    How can I figure out which button the mouse is hovering over? I want to do something similar to a tooltip except I would display/hide a whole window with text, textures, and the whole shabang. However each button needs a different window with different info and textures and I cant figure out how to identify a GUI button (unity 2.0)
    Thanks,
    Luke
     
  2. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    It would be good to have that but afaik it can't be done, you can change the texture and so on but not make a certain script trigger when the mouse rolls over.

    Put it on the wish list
     
  3. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    This is not entirely true. There was a hack floating about the forum, which uses the tooltip to detect which component the mouse is over.

    I use it to play sounds on mouse over.

    Regards,
    Marc
     
  4. Marc

    Marc

    Joined:
    Oct 4, 2007
    Posts:
    499
    Here is an example of one way to do it.

    Code (csharp):
    1.  
    2. void OnGUI() {
    3.         GUIContent content = new GUIContent("The Button", "mouseover-button");
    4.        
    5.         if(GUI.Button(new Rect(0, 0, 100, 100), content) ) {
    6.            
    7.         }
    8.        
    9.        
    10.         if(GUI.tooltip.Equals("mouseover-button")) {
    11.             // Play sound
    12.         }
    13. }
    14.  
    The above code might execute your playsound to much, so maybe its a good idea to add a check that only plays it when the event type is repaint.
     
  5. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Hey i did not know it was able to do that, can you do it in java? if yes can you translate it to java i am not so good at c.
     
  6. JohnGalt

    JohnGalt

    Joined:
    Nov 27, 2007
    Posts:
    85
    My two cents:

    Code (csharp):
    1.  
    2.     if (Event.current.type == EventType.Repaint)
    3.     {
    4.         if (!GUI.tooltip.Equals(mLastTooltip))
    5.         {
    6.             if (!GUI.tooltip.Equals(""))
    7.                 PlayRollOverSound();
    8.             mLastTooltip = GUI.tooltip;
    9.         }      
    10.     }