Get CharacterInfo in the Editor?
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 [h]: 0.000000
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.
Possibly a long term problem .. see post at the end http://forum.unity3d.com/threads/font-getcharacterinfo-bug-in-wp8-win8-for-non-latin-character.200570/
Looks like a duplicate of this one: http://answers.unity3d.com/questions/967826/getting-width-of-a-character-in-new-46-ui-system.html
No one has answered except for $$anonymous$$organs stop gap solution :(
Answer by demented_hedgehog · Sep 18, 2015 at 11:07 AM
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.
1) 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!
2) 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 http://docs.unity3d.com/ScriptReference/Font.RequestCharactersInTexture.html
Thnx!
Font.RequestCharactersInTexture(string, fontsize);
Was exactly what I needed! :)
Your answer
Follow this Question
Related Questions
How do I space out UI elements evenly in a horizontal layout group? 1 Answer
Problem with dynamic height elements inside Vertical Layout Group 0 Answers
Ui layout elements keep reverting after taken outside of grid element 0 Answers
Issues with layout after building 0 Answers
How to make child of layout group always fit the screen size and be placed side by side? 1 Answer