Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TextMesh Pro Saving and Loading TMP_FontAsset To/From Disk

Discussion in 'UGUI & TextMesh Pro' started by Clonze, Mar 1, 2021.

  1. Clonze

    Clonze

    Joined:
    Dec 15, 2012
    Posts:
    26
    Hello! This might be a stretch looking for a solution with this one...

    Wondering if anyone has any experience with saving/loading TMP_FontAssets. I'm trying to save the data after creating a TMP_FontAsset during build runtime, and then load it back in again.

    I'm trying to avoid the taxing process of rebuilding the textmeshpro font atlas everytime.

    I couldn't find any information on this after searching online and through documentation. I saw some code related to saving stuff, but it was in the Editor scripts for TMPro, but it seemed dependent on UnityEngine.Editor.
     
  2. giantkilleroverunity3d

    giantkilleroverunity3d

    Joined:
    Feb 28, 2014
    Posts:
    383
    Hello,
    It is good you ask this. I dont have an answer specific to this effort but I have been through many assets trying this very same thing. Building something dynamic and storing for static. There will be other problems in this effort. One attempt I found was in producing arrays of gameobjects. The start up was a crawl so I stored the first pass in an sql db. Then in another scene away from the main game I produced the gameobject with different generation parameters and prefabed them. When my simulation starts up it is switched to use the prefabs and I build the next set in a coroutine while the main scene executes.
    I hope this helps in other ways. Just a thought.
    So here is a performance review of the model at 50 GOs. I have pumped this up top 1000 to see the choke factor.
    https://fb.watch/3YSOYHtOrC/
     
  3. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you provide more insight on the use case?
     
  4. Clonze

    Clonze

    Joined:
    Dec 15, 2012
    Posts:
    26
    Sure, my application allows the end user to customize various things, images, animations, sounds, all of these can be added at build runtime. I'm now trying to add text customization.
    I want them to have the capability to select an installed font, turn it into a TMPro_FontAsset, so they can adjust various things like character spacing, add outline, and all that fancy stuff. (this all works)

    Now for the problem: Generating the font atlas.
    It's too slow and too inefficient to be a repeated task. If I could save the texture atlas and required data, I could just load it all back up seamlessly.

    When my App/Game is running, after hitting play, there's no load screens... at any point a custom font could be requested to be loaded, and I cannot just put a load screen while the atlas builds.

    Most applications build the required assets in the editor, my application does it during build runtime because it's all about user customization.
     
  5. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    How so?

    Creating a new font object and dynamic font asset at runtime using something similar to the code below should be pretty efficient.

    Code (csharp):
    1.  
    2. // Get paths to OS Fonts
    3. string[] fontPaths = Font.GetPathsToOSFonts();
    4.  
    5. // Create new font object from one of those paths
    6. Font osFont = new Font(fontPaths[index]);
    7.  
    8. // Create new dynamic font asset
    9. TMP_FontAsset fontAsset = TMP_FontAsset.CreateFontAsset(osFont);
    10.  
    Have you tried using something similar to this?
     
    Last edited: Mar 2, 2021
  6. Clonze

    Clonze

    Joined:
    Dec 15, 2012
    Posts:
    26
    I am using that, I just thought it would become a little laggy once your start adding different language unicodes (such as chinese) and also having several fonts that need to be created in quick succession.

    I have tested, and the the app has to wait for the task to complete, and it wasn't really an extreme case, I think...
     
    Last edited: Mar 2, 2021
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Creating the font object(s) and font asset(s) should be very fast unless you are trying to prepopulate those font assets with lots of characters.

    Are you trying to pre-populate those font assets with characters?

    P.S. Asking all these questions so I can get a better understanding + currently working on Dynamic OS Font Assets.
     
  8. Clonze

    Clonze

    Joined:
    Dec 15, 2012
    Posts:
    26
    Yes potentially. But if there's no reasonable solution to saving the font with those characters, I'll have to cut that feature. My application supports many languages and cutting this feature would not be ideal.
     
  9. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Regardless of the supported languages, I would recommend you only add / prepopulate the font asset with known characters needed to display your menus / UI. Ie. I would not prepopulate the font assets with characters expected to come from user input as the dynamic system is plenty fast to handle those at runtime.

    What kind of performance are you seeing?

    Is this on desktop or mobile platforms?
     
  10. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    You can already create / use static font assets for all the known characters. Ie. again only those present in your menus, dialogues, etc. but excluding characters coming from user input. These provide the best performance but increase build size or potentially requires using AssetBundles to deliver those payloads.

    Having said that, the dynamic system should be plenty fast to handle all of this.

    Since I am currently working on those system anyway, I'll do some tests creating font assets for each of the fonts present on my Windows system and display text using each of them to see what type of overall performance I get. I can repeat the process on Mobile as well.
     
  11. Clonze

    Clonze

    Joined:
    Dec 15, 2012
    Posts:
    26
    I'm on windows. I'll try to make use of what I have now. Thanks so much for your help, you're awesome! :)
     
  12. Appectra

    Appectra

    Joined:
    Dec 23, 2022
    Posts:
    1
    Hi,

    I know this an old post but if somebody can help here, I will really appreciate. I need to load Korean font at runtime. For this I have added the Korean font file in the Resources folder and at runtime I am loading that font and creating TMP Font Asset like above. It works fine when I run my game in Unity Editor, but when I build and run it on an Android device, it does not load the Korean font. Rather it shows only boxes (usual thing when a character is not found). Why this is happening?

    Thanks