- Home /
Displaying a tooltip with GUILayout.Window
I'm having trouble getting GUI.tooltip to work well with GUI Windows and GUILayout. There are two issues:
It seems that GUI.tooltip is only valid inside the Window's GUI function, but I need to be able to show a tooltip that extends outside the boundaries of the Window.
The tooltip needs to display even if the window is not focused.
Because I'm using GUILayout instead of GUI, I can't check if the mouse position is inside the component's rectangle.If anyone could show me how to get the below code working, it would be hugely appreciated!
using UnityEngine;
public class App : MonoBehaviour {
Rect win1Rect = new Rect(20, 20, 150, 150);
void OnGUI () {
GUILayout.BeginArea(win1Rect);
win1Rect = GUI.Window(0, win1Rect, DoWin1, "Win1");
GUILayout.EndArea();
if (GUI.tooltip != "") {
// Does not work because it's outside of DoWin1
GUI.Label(new Rect(0, 0, 100, 20), GUI.tooltip);
}
}
void DoWin1 (int windowId) {
GUILayout.BeginVertical();
GUILayout.Button(new GUIContent("Button", "Win1 tooltip"));
GUILayout.EndVertical();
}
}
Answer by Bunny83 · Aug 28, 2011 at 12:26 PM
GUI.Window (You should actually use GUILayout.Window) works a bit different than other GUI controls. Unity "saves" the data you provide (id,rect,callback, title) internally and handles the windows after OnGUI is finished. That's why you have to use a callback function so Unity can execute the window stuff when it needs to.
To access the tooltip from inside a window outside you have to store it somewhere outside.
Just do something like:
using UnityEngine;
public class App : MonoBehaviour { Rect win1Rect = new Rect(20, 20, 150, 150); string win1ToolTip = "";
void OnGUI () {
GUILayout.BeginArea(win1Rect);
win1Rect = GUI.Window(0, win1Rect, DoWin1, "Win1");
GUILayout.EndArea();
if (win1ToolTip != "") {
// Does not work because it's outside of DoWin1
GUI.Label(new Rect(0, 0, 100, 20), win1ToolTip );
}
}
void DoWin1 (int windowId) {
GUILayout.BeginVertical();
GUILayout.Button(new GUIContent("Button", "Win1 tooltip"));
GUILayout.EndVertical();
if (Event.current.type == EventType.Repaint)
win1ToolTip = GUI.tooltip;
}
}
It will delay the reaction by one frame because the tooltip change will take affect next frame but usually you can't see such a delay.
Thanks for taking the time to respond. Unfortunately, this code didn't work for me (even after changing the label to use win1ToolTip ins$$anonymous$$d of GUI.tooltip). However, what I ended up doing is only setting the value of win1ToolTip if onGUI is in the repaint frame: if (Event.current.type == EventType.Repaint).
I guess now it will be a matter of creating a new window that holds this tooltip, and making sure it always renders on top of all other GUI components.
Thanks for pointing me in the right direction.
Sorry for the typo ;) and yes, you're right you should only read the tooltip in repaint. I've edited my answer.
This just helped me a lot. It would've taken me ages to figure out why the tooltip was empty outside the window without this. :)
Your answer
Follow this Question
Related Questions
Why can't I get my tooltip to show only when there is a tooltip set? 2 Answers
GUI.Window error. InvalidOperationException: Hashtable.Enumerator: snapshot out of sync. 0 Answers
Having a GUILayout Window position immediately at screen center 5 Answers
Windows that do not take priority over other GUI elements 0 Answers
GUI Window Render Priority 1 Answer