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
0
Question by Kensei · Mar 28, 2014 at 06:46 PM · guistringonguidisplaylength

Display text with unknown length?

What should I use to display a string with an unknown amount of characters to me? This is the code I wrote for the label.

 GUI.Label (new Rect(920,20,100,20), ((int)energy).ToString());
 GUI.Label (new Rect(920,60,100,25), ((int)generator).ToString() + "  J/s");

Problem is when the counter gets too big, instead of clipping through the label it gets cut off by its border. I would try GUI.Box but I don't want to waste clicks on it. Maybe I should ditch uGUI for the label and use 3D Text? Or GUI Text? Or maybe I'm missing some property in onGUI? I'm so confused at this point, too many UI's, and I can't find one that doesn't restrict the length of a string.

Edit: I also tried doing this with a Rect, but I can't stick an int.ToString as a string param. I think I really will just ditch uGUI and get some 3D text going. Still, just out of curiosity I would like to know how exactly to make a label that automatically resizes itself relative to the content it displays.

 Rect lbl = GUILayoutUtility.GetRect (new GUIContent("text"),GUIStyle.none);
 GUI.Label (new Rect(920,20,lbl.width,lbl.height), ((int)energy).ToString());

This sort of works but you need to specify max characters. A lack of a simple toggle in the Inspector for this is baffling.

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

1 Reply

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

Answer by Jamora · Mar 28, 2014 at 08:06 PM

Sounds like you have some space reserved for the increased counter, so the simplest way is to just make a wider rect in the first place.

If for some reason this is not possible, or you want the exact width for the containing rect, you can also get the width of the string from its GUIStyle.

 GUI.skin.label.CalcSize(new GUIContent( ((int)generator).ToString() + "  J/s" )).x;

I recommend caching the string to avoid creating new strings every frame, as the GUI system for Unity is already quite heavy.

Comment
Add comment · Show 3 · 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 Kensei · Mar 29, 2014 at 03:54 AM 0
Share

Hmm that's weird, this is using UnityEngine right? I don't think I'm missing a namespace, but all I get from GUIStyle. is Equals, RefferenceEquals and none. And the only example in the reference manual is in JS. I can't declare this.

Also, thank you for the caching tip, it didn't occur to me I was creating strings every frame.

Edit: Hmm I can't cache a method, can I?

 private string counter = (int)generator.ToString();

Nvm, I forgot it has to be done in Awake ().

Edit2: I managed to stick it into a float, but now I'm getting a bunch of errors on compile saying I have invalid arguments.

 private float labelWidth = GUI.skin.GetStyle("label").CalcSize(new GUIContent((int)energy.ToString())).x;


Edit3: I also tried doing this with a Rect, but I can't stick an int.ToString as a string param. I think I really will just ditch uGUI and get some 3D text going. Still, just out of curiosity I would like to know how exactly to make a label that automatically resizes itself relative to the content it displays.

 Rect lbl = GUILayoutUtility.GetRect (new GUIContent("text"),GUIStyle.none);
 GUI.Label (new Rect(920,20,lbl.width,lbl.height), ((int)energy).ToString());

This sort of works but you need to specify max characters. A lack of a simple toggle in the Inspector for this is baffling.

avatar image Jamora · Mar 29, 2014 at 12:20 PM 0
Share

Didn't notice the function wasn't static. I corrected my answer.

 temp = GUILayout.TextField(temp,GUILayout.Width(40f));
 float width = GUI.skin.label.CalcSize(new GUIContent(temp)).x;
 GUI.Label(new Rect(100f,100f,width,20f),temp,"Box");

Works for me. The Box style is just for visual confirmation that the rect is actually being resized.

avatar image Kensei · Mar 29, 2014 at 02:50 PM 0
Share

Seems to be working here as well...I even went ahead and did it with a GUI Text mesh -.- Anyway, thanks..I think I'll wait for the new GUI in Unity 5 for my next GUI endeavor. Thanks again.

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Display problem with OnGUI 3 Answers

GUI Label problem 1 Answer

Using Polish characters in OnGUI 1 Answer

onGUI style silly problem 1 Answer

Problem Displaying Score on GUI text 0 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