Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

TextMesh Pro Creating TMP font during build runtime

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

  1. Clonze

    Clonze

    Joined:
    Dec 15, 2012
    Posts:
    26
    Hi, I'm looking for a solution to read a .ttf file as bytes[] and somehow turn it into a TMPro FontAsset. I've spent a few hours and decided to move on to creating a TMPro FontAsset with Font.CreateDynamicFontFromOSFont in combination with TMP_FontAsset.CreateFontAsset, however I can't seem to get that working either.

    Maybe I can get access to unity source code for importing a UnityEngine.Font to use with TMP_FontAsset.CreateFontAsset? I have Unity Pro.

    Hoping anyone can shed some light on this, I'd really love to have this as a feature of my product, and the end users would love it.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    The Font.CreateDynamicFontFromOSFont() does not create a valid font object in the sense that it does not contain the font data.

    Instead use the following as per this post.

    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.  
     
    Clonze likes this.
  3. Clonze

    Clonze

    Joined:
    Dec 15, 2012
    Posts:
    26
    Thanks a bunch! it works! :D
    I think I got confused when the new Font(string) Intellisense was asking for a "name" but looks like it uses a file path?
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    Correct.

    P.S. Check out the following post about Dynamic OS Font Assets which will be available in the next preview releases.

    upload_2021-2-28_22-32-35.png
     
  5. Clonze

    Clonze

    Joined:
    Dec 15, 2012
    Posts:
    26
    thanks for the update, I'm curious if that will fix the following issue that I noticed:

    Only certain fonts can be used with this method of OSFonts. I'm not sure why.
    How can I allow fonts that I install to show up? I'm trying to use fonts that are listed as Free for Commercial use.

    **** Nevermind, I think i figured that out.
    Only fonts with Font file names can be used! it works great again! :p
     
    Last edited: Mar 1, 2021