- Home /
Getting width of word in new UI system: GetCharacterInfo?
I need to get the rendered width of a word (InputField or Text object) in the new UI system (4.6), to implement my own word-wrapping system. (Aside: the built in system will split a long word in the middle if it has to, but I want to shrink the font instead.)
The first two lines of my code (JavaScript/UnityScript) run without error. The third line is the problem:
var ci : CharacterInfo;
dotFont.GetCharacterInfo( "T"[0], ci, 32 ); //Font size 32
Debug.Log (ci.advance);
I want it to tell me the rendered width of a capital letter "T" at 32-point size. (dotFont is assigned in the Inspector, and renders fine visually.)
But I get this error: BCE0019: 'advance' is not a member of 'UnityEngine.CharacterInfo'.
Same error if I try "glyphWidth" instead of "advance". But those are all listed here:
http://docs.unity3d.com/ScriptReference/CharacterInfo.html
What might I be doing wrong? How can I access CharacterInfo details properly from JavaScript? (Or is there a better approach to getting the width of a word? My last resort will be to measure the characters in Photoshop and make my own LUT. Yuck!)
Thanks in advance! (No pun intended.)
Sorry--edited to add that little detail! I'm in 4.6 Pro. (Approaching 5 with caution.)
Answer by steakpinball · May 13, 2015 at 01:46 PM
Use CharacterInfo.width
instead. CharacterInfo.advance
was added in Unity 5 to replace width
. It isn't available in 4.6. The properties available in 4.6 are flipped
, index
, size
, style
, uv
, vert
, and width
.
http://docs.unity3d.com/462/Documentation/ScriptReference/CharacterInfo.html
You can always find the documentation for your version of Unity installed on you computer. Click the little book icon on any component to open it. (Tip 6 on the tips post)
Thanks! .width is indeed accepted without error. (Nice to know about adding 462/Documentation/ to the docs path, too.)
However, it always returns a width of 0 for any character at any font size.
I notice the documentation for Font talks only about GUI Text and Text $$anonymous$$eshes and textures. No mention of the new 4.6 UI system. Is word/character width measurement even possible with the new system (Text and InputField elements)?
(I'll start a new question since I can't re-activate this one.)
Your answer
![](https://koobas.hobune.stream/wayback/20220613195446im_/https://answers.unity.com/themes/thub/images/avi.jpg)