• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by GlennHeckman · Oct 25, 2010 at 04:17 AM · guilabeltooltip

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!

Comment
Westland

People who like this

1 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Slem · May 16, 2011 at 06:00 PM 0
Share

Check out GUILayoutUtility.GetRect()

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by geekedatbirth · Nov 13, 2011 at 03:23 AM

I am no Pro Unity programmer by any means, but I found your question w$$anonymous$$le searc$$anonymous$$ng for the same answer myself, and I figured that since I've found an answer since then I would share... even though t$$anonymous$$s question is over a year old, someone may find it again someday.

I'm sure there are better ways to do t$$anonymous$$s, 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;
 }

T$$anonymous$$s 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 t$$anonymous$$s, but t$$anonymous$$s works for me... my apologies in advance if I forgot somet$$anonymous$$ng but I'm still relatively new to Unity, especially via c#

Comment
Westland

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Trouble using C# tooltips 1 Answer

Text Alignment 2 Answers

OnGUI called after LateUpdate screwing up debug text database 1 Answer

GUILabel doesn't show up 1 Answer

Score display not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges