I’m trying to lay out some text in a MonoBehaviour script. Now the font knows its name “Arial” and its size 13 but when I do this:
CharacterInfo info;
foreach (char c in text) {
font.GetCharacterInfo (c, out info);
Debug.Log ("Char is [" + c.ToString() + "]: " + info.advance.ToString("F6"));
}
GetCharacterInfo is returning False and I get this output:
Char is [T]: 0.000000
Char is <mark>: 0.000000</mark>
Char is [e]: 0.000000
Char is : 0.000000
So my questions are: “Why is this so?”, and “What can I do to get this information?”
Note that the monobehaviour has the [ExececuteInEditor] flag set although it doesn’t work when I play the game either. Also in Unity 5.2.0f3 on wine if that matters.
OK… user error here. There’s a mismatch between the fontsizes. I don’t know where the font size of 13 is coming from but if I change the font size in GetCharacterInfo to 14 which is the font size of the UI.Text field I get non zero values for advance.
More info. Areas of confusion.
-
Font has a fontSize. UI.Text has a fontSize and a font.
Setting a UI.Text instances font does not set the UI.Texts fontSize to the fonts fontSize!
-
Had I RTFM then I would have noticed that before the character info is available you have to call RequestCharactersInTexture(…) to make sure that the character info is included. See Unity - Scripting API: Font.RequestCharactersInTexture