- Home /
How to prepopulate Font Cache?
Changing Texts on the GUI has some negative effect on the CPU Speed. ComputationSpikes are the result. Especially calls to Font.CacheFontForText if the Profiler can be trusted on this. 368.55ms for 2 Calls to this function...
Answer by Graham-Dunnett · Jul 28, 2011 at 08:40 PM
You must be using a dynamic texture. With dynamic textures Unity renders glyphs into a texture as you submit character strings for rendering. Unity effectively manages a texture with all the fonts that you have used in all the different sizes. Font.CacheFontForText is basically the code that is trying to find where in this texture to put the new glyphs. Occasionally it runs out of space in the texture, in which case the texture is replaced by one twice the size and all the glyphs from the old texture are copied across into the new one. Also, find space for glyphs (which might all be different sizes) can take time.
To pre-populate the cache you might want to try and render as much of your text, perhaps by rendering GUITexts onto a render texture (or something that you are not going to display. I guess a different approach is rendering one single glyph that is super ginormous in size to force the dynamic font texture to be enlarged up to the size you need it to be.
Answer by jonas-echterhoff · Jul 29, 2011 at 09:50 AM
What Graham said is all correct. Also, you might consider just using a static Font texture (change the font import settings from "Dynamic" to one of the other settings). Especially, if you don't need the flexibility of displaying asian character sets or very big characters, then you might not need a dynamic font.
You're my hero! :D I'd upvote this very very much, if only I had 15 reputation xD
Answer by IVxIV · Dec 24, 2014 at 08:40 PM
If you know ahead of time what dynamic font(s) you are going to be using, one approach that I have used with success in the past is to make repeated calls to Font.RequestCharactersInTexture() inside of a coroutine (to spread the work of precaching font glyphs across several frames). Here is some example code illustrating the approach I have 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
}
};
Answer by ickydime · Oct 29, 2015 at 06:25 PM
I modified @IVxIV's script so you can just add the following to entities with Text behaviors.
You'll need to create your own GlyphSet enum and constants but that is pretty straight forward.
public class FontCacher : MonoBehaviour {
public Text TextField;
public GameConstants.GlyphSets GlyphSet = GameConstants.GlyphSets.Standard;
public string CustomGlyphs;
protected void Awake () {
if(TextField==null)
{
TextField = gameObject.GetComponent<Text>();
}
StartCoroutine(CacheFont());
}
protected IEnumerator CacheFont()
{
string glyphs;
switch(GlyphSet)
{
case(GameConstants.GlyphSets.Custom):
glyphs = CustomGlyphs;
break;
case(GameConstants.GlyphSets.Current):
glyphs = TextField.text;
break;
case(GameConstants.GlyphSets.NumbersOnly):
glyphs = GameConstants.GLYPH_NUMBERS;
break;
case(GameConstants.GlyphSets.Standard):
default:
glyphs = GameConstants.GLYPH_STANDARDS;
break;
}
Font font = TextField.font;
font.RequestCharactersInTexture(glyphs, TextField.fontSize, TextField.fontStyle);
yield break;
}
}