Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

GUI.tooltip unexpected behaviour on mouse hover/click?

Discussion in 'Immediate Mode GUI (IMGUI)' started by Mat, Apr 9, 2010.

  1. Mat

    Mat

    Joined:
    Dec 4, 2009
    Posts:
    30
    Hello,


    Please consider the script listed below attached to the main camera in an empty Unity scene...


    Code (csharp):
    1. using UnityEngine;
    2.  
    3.  
    4. public class GuiTooltipTest : MonoBehaviour
    5. {
    6.     private void OnGUI()
    7.     {
    8.         if ( GUI.Button( new Rect( 100, 100, 200, 60 ), new GUIContent( "The button caption", "Has mouse focus!" ) ) )
    9.             Debug.Log( "Button clicked!" );
    10.  
    11.         Debug.Log("GUI.tooltip value: \""+GUI.tooltip+"\"_________________time:"+Time.fixedTime);
    12.     }
    13. }

    ...and the output it produces...

    • ...when the mouse is hovering over the button:
      Code (csharp):
      1. GUI.tooltip value: "Has mouse focus!"_________________time:1.02
      2. GUI.tooltip value: ""_________________time:1.04
      3. GUI.tooltip value: "Has mouse focus!"_________________time:1.04
      4. GUI.tooltip value: ""_________________time:1.06
      5. GUI.tooltip value: "Has mouse focus!"_________________time:1.06
      6. GUI.tooltip value: ""_________________time:1.08
      7. GUI.tooltip value: "Has mouse focus!"_________________time:1.08
      8. GUI.tooltip value: ""_________________time:1.1
      and so on...
    • ...when the mouse button is held down over the button ('MouseDown' event):
      Code (csharp):
      1. GUI.tooltip value: ""_________________time:2.68
      2. GUI.tooltip value: ""_________________time:2.7
      3. GUI.tooltip value: ""_________________time:2.72
      4. GUI.tooltip value: ""_________________time:2.74
      5. GUI.tooltip value: ""_________________time:2.76
      6. GUI.tooltip value: ""_________________time:2.78
      7. GUI.tooltip value: ""_________________time:2.8
      8. GUI.tooltip value: ""_________________time:2.82
      and so on...

    Please notice that the time is displayed to ensure the string reaches the output, otherwise Unity somehow filters debug strings which are the same.


    So, to sum it up: the tooltip is caught only once every two calls in the 'hover' state and simply does not work anymore in the 'active' state. Great! What do you think about it?

    Please tell me that I'm doing something wrong because I am getting really fed up with such strange and undocumented little behaviours spread all over Unity, and especially over UnityGUI. I'm okay with small bugs most of the time but come on, some of these features have been around for two or three years!
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The code in the OnGUI function is called more than once for each frame update. This is not usually noticeable when the code is used purely for GUI display purposes but sometimes makes a difference with code that does something that isn't GUI-related on each call. I don't think the tooltip text is supposed to be displayed when the mouse button is down (at least I can't remember seeing a button that ever had that behaviour), so that would explain why the text is blank during the mouse down phase.
     
  3. Mat

    Mat

    Joined:
    Dec 4, 2009
    Posts:
    30
    Thank you for your reply andeeee. I'd like the developers to explain better in the documentation or somewhere else how exactly the Unity game loop works, or maybe I overlooked it. That or define a clear behaviour for the GUI.

    Technically in the 'active' state, the control can still be hovered by the cursor, the decision to display the tooltip should be left to the user.

    In this case I use GUI.tooltip as a way ('hack' would be more appropriate) to tell whether a control is hovered to disable mouse click events on the GUITexture elements it overlaps.
     
  4. wgrand

    wgrand

    Guest

    Joined:
    Mar 11, 2010
    Posts:
    14
    Did you find a workaround for this hack?
     
  5. wgrand

    wgrand

    Guest

    Joined:
    Mar 11, 2010
    Posts:
    14
    I found a workaround:

    Code (csharp):
    1. private string _hover = "";
    2.  
    3. void Update()
    4. {
    5.   // do something with _hover
    6. }
    7.  
    8. void OnGUI()
    9. {
    10.   _hover = GUI.tooltip;
    11. }