- Home /
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.
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.
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.
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.
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
Follow this Question
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