Inconsistent results with Font.GetCharacterInfo()
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] == ' ')
{
break;
}
bool result = usedFont.GetCharacterInfo(currentTextSpew[i], out currentInfo, fontSize, fontStyle);
if (!result)
{
failedChars.Add(currentTextSpew[i]);
}
else
{
if (failedChars.Contains(currentTextSpew[i]))
{
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);
if (result)
{
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. $$anonymous$$y 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.
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.
Your answer
Follow this Question
Related Questions
Custom font all characters on top of each other 1 Answer
How can I change the UV's on a font and then save it so that these changes are permanent? 0 Answers
font bug, white background under text 1 Answer
Text Mesh Pro no outline settings on costume font 0 Answers
Implicit casting scriptable object to font in unity editor 0 Answers