Ok, so, I feel like I'm going insane. I'm just recently upgraded to Unity 5.2 and I'm getting very inconsistent results with GetCharacterInfo().
I've been using it for a conversation system to get the width of my words so they preemptively adjust themselves as they display letter by letter and don't overflow the text box or auto-adjust mid-word.
I've started noticing inconsistencies with this implementation, but it seemed to be working just fine in the past (it's also just possible I've been stressing it more recently).
So, I have this block of code that hopefully explains my issue:
for (int i = spewPos; i < currentTextSpew.Length; i++)
if (currentTextSpew[i] == ' ')
bool result = usedFont.GetCharacterInfo(currentTextSpew[i], out currentInfo, fontSize, fontStyle);
print("A character that previously failed is now returning a valid result.");
foreach (char failedChar in failedChars)
result = usedFont.GetCharacterInfo(failedChar, out currentInfo, fontSize, fontStyle);
print ("This character used to fail GetCharacterInfo but now does not: " + failedChar);
In English, I'm seeing GetCharacterInfo() fail to return results for a specific character, but then correctly returning results for that character at a later point in time. If GetCharacterInfo() fails to return results for a specific character, it will continue to fail if the frame has not advanced, but after more frames have passed, it will eventually succeed again.
Am I doing something horribly wrong here? Do I need to mess with font import settings to preload this information? I'm happy to provide more specific info about what I'm doing if needed.
Any help at all would be greatly appreciated!
Just FYI, I have more information. The specific issue seems to be that GetCharacterInfo() does NOT return valid results for any character that hasn't yet been rendered.
As soon as a character has been rendered, that function works perfectly. My workaround right now is to set the text of a hidden textbox with the same properties as the one I'm trying to use to a string of every possible character before I actually try to show anything for real.
This feels pretty gross to me though, so I'd still love to hear any other thoughts people might have. This feels like the sort of thing that I should be able to work around with an import setting somewhere.
I'm a bit late to the game here, but @ActualDog, you should try adding Canvas.ForceUpdateCanvases(); just prior to the GetCharacterInfo() calculation, this did work for me, where in some cases it wouldn't evaluate the size correctly. Now it does every time.
It's works. And forcing canvas to update too. You should post as answer and mark your own answer as the solution so this question remains as solved.
I have the same problem. But I noticed If I stop unity and then rerun it immediately I would get the result from GetCharacterInfo. I tried Canvas.ForceUpdateCanvases(); but it did not help.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
34 People are following this question.
Why font does not get enough characters when typing machine , but getting enough to copy and paste this characters!
How to load font into variable
Making a translatable custom font
font bug, white background under text
Custom font all characters on top of each other