• 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
3
Question by yoyo · Dec 03, 2010 at 08:30 PM · guitextmeasurefun

How long is a string?

"How long is a piece of string?" may be a rhetorical question, but I'm hoping there's an answer to "How many pixels long will my string be when rendered into a GUI.Box?"

I want to grow my box to make it big enough to fit my string. GUI.skin.box.lineHeight * numberOfLines tells me how tall it will be, but there's no equivalent for character width. (And since not all characters are the same width t$$anonymous$$s wouldn't work anyway.)

What I want is somet$$anonymous$$ng like the MeasureText methods in Windows Forms.

As a workaround for now I'll split the string into lines, find the longest line, and multiply by an average character width.

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 yoyo · Dec 06, 2010 at 08:32 PM 0
Share

Thanks all -- obviously I didn't RTFM closely enough! I was busy looking at GUIStyle variables and neglected to review the functions, my bad.

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by skovacs1 · Dec 03, 2010 at 09:44 PM

You use GUIStyle.CalcSize:

private var content : GUIContent;

function Start() { content = new GUIContent("T$$anonymous$$s is a Test"); }

function OnGUI() { var boxContentSize : Vector2 = GUI.skin.box.CalcSize(content); GUI.Box(Rect(0,0,boxContentSize.x,boxContentSize.y),content.text); var tfContentSize : Vector2 = GUI.skin.textField.CalcSize(content); content.text = GUI.TextField(Rect(0,boxContentSize.y, tfContentSize.x,tfContentSize.y), content.text); }

T$$anonymous$$s function does not take wordwrapping into account. To do that, you need to determine the allocated width and then call CalcHeight to figure out the wordwrapped height.

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 betaFlux · Jan 04, 2014 at 09:26 PM 0
Share

Invaluable to me! Thanks!

avatar image
1
Best Answer

Answer by Maarten · Dec 03, 2010 at 09:35 PM

You can use GUIStyle.CalcHeight, or GUIStyle.CalcSize for it.

Comment
Add comment · 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
0

Answer by franky303 · Sep 20, 2012 at 11:23 AM

             string str = "";
             string line = "";
             string[] words = caption2.Split(' ');
             for ( int w=0; w<words.Length; w++ )
             {
                 Vector2 size = GUI.skin.label.CalcSize(new GUIContent(line));
                 Debug.Log ( size.x + ":" + size.y );
                 if ( size.x >= (540 - xpadding*2 - 10) )
                 {
                     line = words[w];
                     str += "\n\n" + words[w];
                 }
                 else
                 {
                     if ( line != "" ) line += " ";
                     if ( line != "" ) str += " ";
                     str += words[w];
                     line += words[w];        
                 }
             }
 
             GUI.Label( new Rect(xoff + xpadding,ypadding,542-xpadding*2,540-ypadding), str );
 
Comment
Add comment · 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

2 People are following this question.

avatar image avatar image

Related Questions

Text appear on screen when I stand over object. 1 Answer

How can I add "..." to GUI.Label if it goes past its bounds? 1 Answer

Checking 2 string variables 0 Answers

How to make text that scrolls horizontally? 2 Answers

OnGUI called after LateUpdate screwing up debug text database 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