- Home /
Font.GetCharacterInfo always returns false
Hi, I am trying to get width of a char from unity text component
Text t = GetComponent<Text>();
t.text = "abc";
CharacterInfo info;
if(t.font.GetCharacterInfo('a', out info, t.fontSize)){
//Never returns true
}
I have tried unity default Arial font and many other but none of them successfully return true. What might be the problem?
If the character ch with the specified size and style is present in the font texture, then this method will return true, and info will contain the texture placement information for that character. If the character is not present, this method returns false. If size is zero, it will use the default size for the font. Reference: https://docs.unity3d.com/ScriptReference/Font.GetCharacterInfo.html
Did you test my example or just reposted the docs? Character 'a' is clearly in default Arial font. I even debugged font.characterInfo array which in fact prints 'a' as existing character, but GetCharacterInfo still returns false.
Answer by Ryunigia · Oct 01, 2018 at 01:53 PM
Not sure if still is valid, but you gotta set the font to "unicode" in the import settings or atleast anything other than "dynamic". Atleast that's what i did wrong first
Answer by GGsparta · Nov 23, 2018 at 10:59 AM
Maybe your font is dynamic: this means you have to add characters in your font, and you should use Font.RequestCharactersInTexture! This happened to me with a single UnityEngine.UI.Text component, no matter the font. Reference: https://docs.unity3d.com/ScriptReference/Font.RequestCharactersInTexture.html
Answer by Spring_Heng · Jan 29, 2021 at 07:42 AM
the unity default font is dynamic // answer: Text t = GetComponent(); t.text = "abc";
CharacterInfo info;
font.RequestCharactersInTexture(t.text, t.fontSize,t.fontStyle); //add this
if(t.font.GetCharacterInfo('a', out info, t.fontSize)){
//Never returns true
}