Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
1
Question by Robert 1 · May 21, 2010 at 02:27 PM · guilayouttooltip

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:

  1. 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.
  2. 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.

Comment
Add comment · 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 Molix · May 22, 2010 at 02:04 AM 0
Share

I see, I thought the question was with sizing; I tested with a different string for convenience.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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).

Comment
Add comment · Show 1 · 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
avatar image Robert 1 · May 21, 2010 at 06:58 PM 0
Share

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

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


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