Search Unity

TextMesh Pro Saving / loading font atlas for TextMeshPro Font assets during runtime

Discussion in 'UGUI & TextMesh Pro' started by Victoria-F, Aug 22, 2022.

  1. Victoria-F

    Victoria-F

    Joined:
    Sep 27, 2013
    Posts:
    9
    Hi,
    I am trying to implement a feature in my project where the user creates a redistributable "language pack" using any of their desired font.
    Since using dynamic population mode to generate every character during runtime can be quite expensive, I don't want the font generation to happen on the loading side. (It took around 5 minutes just to generate Japanese font data in my environment, which had around 7000 characters)
    So I thought if it was possible to supply the pack with pre-generated atlas data for the characters used in the pack.

    The way I am currently trying to achieve this is like the following:
    1. generate atlas for all of the characters used in the language pack
    2. pack the font file, generated atlas, and the additional character info into a redistributable package

    and when the file is loaded by the user,
    1. add the given font file as the fallback font of the font asset
    2. load all of the pre-generated atlas/character data into the font asset
    3. if any additional characters are required (e.g. characters in user names), generate atlas for them and also add them to the package, so it can be reused in another session

    But while I was trying to implement this, I found this method to be impractical, mostly due to setting character data from outside of the TMP package being impossible.

    So this is where I am currently stuck now. Is there any solution for this problem?

    Btw, I would like to avoid using Reflection to force set atlas data for the font assets, mostly because I'm not quite certain if it would work well with IL2CPP compilation.

    Unity Version: 2021.3.7f1
    TextMeshPro Version: 3.0.6
     
  2. Victoria-F

    Victoria-F

    Joined:
    Sep 27, 2013
    Posts:
    9
    I guess this problem could be summarized as: "Is there any way I could save/load dynamically generated atlas textures and character infos, so I don't have to generate the characters every session?"
     
  3. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Why not use Dynamic font assets created at runtime using whatever source font provided by the user?

    And why try to pre-populate those font assets with every possible character when in reality only a small sub set is likely to be used?

    With Multi Atlas Texture enabled on dynamic font assets, it ensures that if needed every single character and glyph present in the font file could be displayed.

    Just trying to get more insight from you on this.
     
  4. Victoria-F

    Victoria-F

    Joined:
    Sep 27, 2013
    Posts:
    9
    Let's say when a user opens up a menu with around 100 non-generated characters (which is a pretty common scenario when using Japanese/Chinese), it roughly takes up 5~10 seconds just to generate those characters. I really don't want this happening every session, especially since this is on my environment (which is supposedly pretty fast) and I'm trying to get my project to also run on low-end machines.

    By providing a pre-generated atlas of at least the characters that are most likely to be used, I thought it could at least avoid those situations where the user has to generate large numbers of characters at once.
     
  5. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Static font assets should be used for all known text for any given language or groups of languages. Known text is all the text contained in your menus, UI, dialogues that is known at build time.

    Most of the time, the known text for any given language is not that many characters even for CJK unless you are creating a novel or text adventure game. Typically, you will have one static font asset for Latin text and one for Japanese, another for Korean and lastly one more for Chinese. Depending on the amount of text, both Chinese Traditional and Simplified can fit in the same static font asset. Most of the time, a 1024 x 1024 atlas texture size will work with good sampling quality.

    Each of these static font assets should have a local fallback that is dynamic to handle any unknown characters coming from user input for instance.

    Adding new characters and glyphs to any of these dynamic font assets as users are typing will be noticeable by them as although this performance overhead is significant on low end devices, it is still much faster than they can type.