Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Different GUI.tooltip for different buttons

Discussion in 'Scripting' started by guitarxe, Jan 2, 2017.

  1. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    Hi folks, quick question. I understand this might be out of scope for the purposes of the OnGUI system, but I'm having a hard time understanding how a GUI.tooltip is assigned to a button.

    The example given from the manual is as follows:

    Code (csharp):
    1.  
    2. void OnGUI () {
    3.         // This line feeds "This is the tooltip" into GUI.tooltip
    4.         GUI.Button (new Rect (10,10,100,20), new GUIContent ("Click me", "This is the tooltip"));
    5.        
    6.         // This line reads and displays the contents of GUI.tooltip
    7.         GUI.Label (new Rect (10,40,100,20), GUI.tooltip);
    8.     }
    9.  
    Which doesn't make sense to me because how does the second statement know which button to assign the tooltip to? What if there are two buttons?

    For example:

    Code (csharp):
    1.  
    2.  
    3. void OnGUI()
    4.     {
    5.         //Button #1
    6.         GUI.Button(new Rect(10, 10, 100, 20), new GUIContent("Click me", "This is the tooltip"));
    7.         GUI.Label(new Rect(10, 40, 100, 20), GUI.tooltip);
    8.  
    9.         //Button #2
    10.         GUI.Button(new Rect(10, 120, 100, 20), new GUIContent("Click me #2", "This is the tooltip #2"));
    11.         GUI.Label(new Rect(10, 160, 100, 20), GUI.tooltip);
    12.     }
    13.  
    14.  
    In the above case, if I hover over the first button, I see both tooltips underneath both buttons with the text from the first tooltip.
    And if I hover over the second button, I see only the second tooltip under the second button only. That is very strange behaviour.

    Any tips on this? I haven't been able to find anything regarding this, no examples in neither manual or script reference, and no google hits.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Don't use OnGUI, it's really old, use the new UI
     
  3. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,657
    The new UI doesn't have a built-in tooltip solution, though...
     
  4. guitarxe

    guitarxe

    Joined:
    Dec 1, 2013
    Posts:
    131
    It's not for game UI, it's just for debug info while building out early prototypes. The tooltips aren't essential, I am just curious how this was intended to be used for more than one button.