Huge delay (4s) from Font.CacheFontForText

I have read some of the top google result for Font.CacheFontForText, I am not convinced the problem is resolved in Unity 5.5

My situation:

  • I use a custom font, Unicode-based with CJK glyphs, so it’s pretty large (16MB).
  • I didn’t use Best Fit for my UI Text.
  • However, I do sometimes use rich text with italic tag, eg.

My problem:

  • On launch, my first screen already shows UI Text object using this font, so it’s cached.
  • But as soon as I switch into a view with italic text, Font.CacheFontForText is called and freezes UI for 4 seconds on my machine.
  • The annoying thing being: this only happens during first play (both in play mode and actual build), subsequent playthrough doesn’t encounter this problem. So it is kinda annoying to troubleshoot.

My question:

To workaround this problem, is it current best practice to use off-screen italic text on the first screen, so that my font, included its italicized variant, will be cached properly?

Also I should note that my font doesn’t actually have an italic variant, so Unity was probably making faux italic for all possible glyphs??

Screenshot for reference:

There’s a great article series about the Unity UI library and how to optimize it.
Specifically, here’s the part that discusses fonts: Optimizing Unity UI - Unity Learn

Perhaps it can help out.

@liortal Thx, it did answer some of my questions. Basically, we need to manage italic glyph cache somehow.

There is still a few unanswered questions, but let me setup my scenario first:

  • Since Font.RequestCharactersInTexture works roughly like subsetting a font into a bitmap texture.
  • And we are mostly working with storyline, so texts are known.

My questions:

  • When doing faux italic text, does Unity create a new font? If so, it explains why my problem only appears when italic text is used (I don’t think rebuilding font atlas will cost 4 seconds, it looks more like loading in a large font, albeit in my case it is Unity creating a faux font style).
  • From a maintenance perspective, wouldn’t the off-canvas UI Text trick be simpler than pushing all characters onto texture? In my case, I don’t really have a large leaderboard, but a few sentences of text on screen. My guess being, the bottleneck isn’t font atlas rebuild, but creating a new font style at runtime. It also explains why the issue only appears on first-run (after each OS restart).

Neither way, off-canvas trick does seem to work in my case (as I can’t reproduce the 4-second freeze with it presents). I am just not certain this is the root cause…

were you able to fix the issue ?

I don’t recall the exact details of the post i linked to (i read it some time ago), but i don’t think Unity generates a font; it generates a texture that is used at runtime with the font glyph data it needs (characters).

Yes, but I use the off-canvas trick mentioned in other posts.

As I believe my issue is not bottleneck on font texture (font atlas) rebuild, but on initial italic font creation (maybe it doesn’t create a new font per se, but displaying italic font for the first time freezes the app for several seconds.)

Since the issue only appear on first run, and has nothing to do with font size. I just display some italic text on launch, so that this freeze happens during launch, thus hidden from users during gameplay…