- Home /
Trouble with implementing tooltip with GUILayout
My goal here is to use GUILayout to render a label that has a background that automatically resizes itself to fit its contents (the text). This the only way that I know of to make sure the background texture is as long as the text.
This label will serve as a tooltip that follows the mouse cursor. There's two main problems here:
- There is no way (that I can see) to provide a position for GUILayout.Label without specifying its dimensions. I've tried GUILayout.BeginArea() but it forces the label to be the same size as the area and does not dynamically size.
- Even when I don't use an Area, I still can't get the label's width to automatically adjust based on the amount of text it has.
Here is some code. This function is called after all of my widgets have been drawn. Some widgets specify a tooltip, some don't.
void DrawTooltip() { float width = 300; float height = 50;
Vector2 pos = UIManager.MousePosition;
pos.x -= Rect.x;
pos.y = pos.y - height - Rect.y;
GUIStyle style = UIManager.Instance.TooltipStyle;
style.alignment = TextAnchor.LowerLeft;
GUILayout.BeginArea( new Rect( pos.x, pos.y, width, height ) );
{
GUILayout.BeginHorizontal();
{
GUILayout.Label( GUI.tooltip, style );
GUILayout.FlexibleSpace();
}
GUILayout.EndHorizontal();
}
GUILayout.EndArea();
}
Anyone know how I can get this working properly?
UPDATE
I figured out why this isn't working. Apparently GUI.tooltip is an empty string when OnGUI is called for Layout only (Check Event.current.type for the "Layout" event). However, on the repaint event, it is the valid tooltip. Because the string (GUI.tooltip) is empty during layout, the widgets cannot size to their content properly.
I see, I thought the question was with sizing; I tested with a different string for convenience.
Answer by Molix · May 21, 2010 at 04:27 PM
What you've got seems pretty close, but you might also need to add some flexible space to the vertical, e.g.
GUILayout.BeginArea( new Rect( pos.x, pos.y, width, height ) );
{
GUILayout.BeginVertical();
{
GUILayout.BeginHorizontal();
{
GUILayout.Label( GUI.tooltip, style );
GUILayout.FlexibleSpace();
}
GUILayout.EndHorizontal();
GUILayout.FlexibleSpace();
}
GUILayout.EndVertical();
}
GUILayout.EndArea();
If the width of the label still does not adjust, make sure your style has "fixedWidth" set to 0 (i.e. no fixed width, allow adjustment).
This still doesn't work. The label's size isn't adjusting when text is added. If I remove the flexible spaces then the text does indeed show up, but it has no affect on the over-all sizing algorithm for some reason. I have also made sure that fixed width/height are set to 0 in the GUIStyle.
Your answer
![](https://koobas.hobune.stream/wayback/20220613013751im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Custom Inspector tooltips behaving inconsistently 0 Answers
GUILayout ArgumentException when combining with GUI.Tooltip 1 Answer
GUI objects (Label and box) change their opacity for unknown reason. 1 Answer
Displaying a tooltip with GUILayout.Window 1 Answer
Why can't I get my tooltip to show only when there is a tooltip set? 2 Answers