- Home /
Dynamic Resizing of tooltip to match content?
Ok, I've read the forums for about 3 hours and I have yet to find a solution, just questions and mild attempts at answers ...
How do (can) you create a tooltip that re-sizes itself dynamically to match the size of the content text?
I know how to create a tooltip, I know how to make it drag to my pointer, but I don't know how to make it work utilizing the GUILayout.Label(GUI.tooltip,"tooltip") approach ... I run into the whole Layout / Repaint issue. (if you have an answer for that, great!)
My GUI has many / various types of buttons and tooltip data that will need to be displayed, but a hard-coded tooltip of 150 x 50 won't do the trick. I found these posts, but I'm not sure how to implement them:
- Background to tooltip - how?
- Seriously... How does GUIStyle.CalcSize() work?
- Is there a way to measure the pixel with/height of a string with a given font?
If anybody has a technique for resizing tooltips to adjust to the content, please let me know. Any help will be much appreciated!
Additionally, if somebody has come up with a way to delay the appearance of a tooltip for a specified period of time, please clue me in!
Thanks!
Answer by geekedatbirth · Nov 13, 2011 at 03:23 AM
I am no Pro Unity programmer by any means, but I found your question while searching for the same answer myself, and I figured that since I've found an answer since then I would share... even though this question is over a year old, someone may find it again someday.
I'm sure there are better ways to do this, but I don't know of any yet. At least it's an answer though.
private Vector2 mousePos;
private float tooltipTime;
private string lastTooltip;
public float maxTooltipTimer = 1.5f;
public GUI.skin toolSkin;
// set to your tool tip skin
// then create a custom style in the GUI Skin above with the name "TestHeight"
// set maxTooltipTimer to your desired delay
// create your tool tip above using the standard GUI.Content() and wallah
// to change the background or font settings to your tool tip, find the
//"TestHeight" custom style in the skin and change accordingly
if(GUI.tooltip != lastTooltip) {
tooltipTime += Time.deltaTime;
if(tooltipTime > maxTooltipTimer) {
float w = GUI.skin.GetStyle("TestHeight").CalcSize(new GUIContent(GUI.tooltip)).x;
float checkW = w - (Screen.width - mousePos.x);
if(checkW < 0) {
checkW = 0;
}
GUI.TextArea (new Rect(mousePos.x - checkW, Screen.height - mousePos.y - 35, w, 20), GUI.tooltip);
}
} else {
if(tooltipTime > 0) {
tooltipTime = 0;
}
}
lastTooltip = GUI.tooltip;
void Update () {
mousePos = Input.mousePosition;
}
This places a tooltip at the cursor position after the set number of seconds with GUISkin and style set to it and adjusts the width of the box for the length of the text. Again, I'm sure there are better ways to do this, but this works for me... my apologies in advance if I forgot something but I'm still relatively new to Unity, especially via c#
Your answer
Follow this Question
Related Questions
Trouble using C# tooltips 1 Answer
Text Alignment 2 Answers
Why can't I get my tooltip to show only when there is a tooltip set? 2 Answers
Gui label variable as a string.. 1 Answer