- Home /
Massive lag due to Font.CacheFontForText. Please help!
I have a massive lag (1128ms) on the Ipad 2, the profiler shows this:
I have already done all the suggestion on the forums: Change font to non dynamic Dont change color from code Dont change size. Also changed the font to Arial, no difference.
Im using NGUI and also 3d Text (TextMesh)
Any other suggestions?
Answer by sschaem · Jun 24, 2014 at 01:09 AM
You can try to create dummy text during your level load time to make sure the dynamic caching does it work then and not at some other importunate times?
If not, you can try "TextMesh Pro", it is optimize to do little to no run-time allocation. Once your font is loaded, you are pretty much guarantied to be alloc free.
And loading a font is also extremely efficient as its using very compact SDF atlas.
Text$$anonymous$$esh Pro looks great, just worried that it might not solve my problem, do you have a demo version I can try out first?
Text$$anonymous$$esh Pro doesnt rely on Font.CacheFontForText, and this should be gone from your profiler report. If not I'm guessing that some object in the scene still rely on the Font class.
You can get in depth tech support at : http://digitalnativestudios.com/forum/
@JACLE$$anonymous$$GO: I am the author of Text$$anonymous$$esh Pro and although the Font.CacheFontForText isn't related to Text$$anonymous$$esh Pro, given you purchased my product, I would like to help you out if you still haven't solved your issue. Please feel free to email: support@DigitalNativeStudios.com
@stephanB problem solved, was Unity Dynamic font. Thank you
Answer by IVxIV · Dec 24, 2014 at 08:37 PM
For anyone else who encounters this same issue, here is another solution you could consider. I ran into the same problem as a result of using custom GUIStyle objects for rendering in my OnGUI calls, some of which referenced custom dynamic fonts (this is the source of the problem - dynamic font glyphs get cached to an internal font cache texture in Unity).
To avoid taking the big hit all at once when Font.CacheFontForText() is first called by means of an OnGUI call, inside an Awake() call for my UI code I spin up a coroutine to asynchronously precache each of my dynamic fonts' glyph data, making repeated calls to Font.RequestCharactersInTexture(). This has worked nicely so far for my relatively simple UI needs, and avoids having one giant blocking stall where the GUI font cache is populated all at once.
Here is some sample code to illustrate the basic approach I used:
public class GUIClass : MonoBehaviour
{
private static readonly string kPrecacheFontGlyphsString= "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_+=~`[]{}|\\:;\"'<>,.?/ ";
// utility struct used in font caching
struct CacheFont
{
public Font theFont;
public int size;
public FontStyle style;
};
void Awake()
{
// gather custom fonts that we will be using
CacheFont[] myCustomFonts= RetrieveMyCustomFonts();
if (null != myCustomFonts)
{
for (int fontIndex= 0; fontIndex < myCustomFonts.Length; ++fontIndex)
{
StartCoroutine(PrecacheFontGlyphs(
myCustomFonts[fontIndex].theFont,
myCustomFonts[fontIndex].fontSize,
myCustomFonts[fontIndex].fontStyle,
kPrecacheFontGlyphsString));
}
}
return;
}
// Precache the font glyphs for the given font data.
// Intended to run asynchronously inside of a coroutine.
IEnumerator PrecacheFontGlyphs(Font theFont, int fontSize, FontStyle style, string glyphs)
{
for (int index= 0; (index < glyphs.Length); ++index)
{
theFont.RequestCharactersInTexture(
glyphs[index].ToString(),
fontSize, style);
yield return null;
}
yield break;
}
void OnGUI()
{
// now that dynamic font glyphs have been precached,
// there won’t be a terrible hitch the first time this is called
}
};
hello, how to solve a the problem when i use NGUI font ?