Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

TextMesh Pro & OS Font

Discussion in 'UGUI & TextMesh Pro' started by llxwd008, Dec 20, 2020.

  1. llxwd008

    llxwd008

    Joined:
    Aug 7, 2015
    Posts:
    49
    Hi,
    I have read the discussion about TMP and OS Font as below:

    // Get the file path of all OS fonts.
    string[] fontPaths = Font.GetPathsToOSFonts();
    // Create new Font using one of those paths.
    Font osFont = new Font(fontPaths[124]);
    // Create new font asset using this OS font.
    TMP_FontAsset fontAsset = TMP_FontAsset.CreateFontAsset(osFont);


    I want to use this to create Font on cross-platform device. But there is still a problem here, the code above list all the fontpath of the OS, but how do I decide which one to use? Is there any way to get the default font used by the OS? Then I can use it directly.

    E.g, if I use the UI.Text, and set font to Arial, Then on cross-platform device, it will use default OS font. Is there similar method for TMP?
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    When using this default Arial, the legacy text system uses some hard coded list to go thru those OS fonts looking for characters and this is different on each platform. In this implementation, you have no control over what font ends up being used.

    Although using TMP requires you learn about what fonts are available on those platforms and select which font you wish to use, you have full control over it all.

    On most platforms, your will find the NotoSans font family which does provide support for most language although from different font file. Ie. on Android you have several NotoSans font files and for different language groups.

    You can learn about this NotoSans font family here.

    You can also check out Google Fonts where you can find fonts per language or cross reference the fonts available on each platform to select which one have what language coverage and which you want to use that have a design style that suites your taste / design.

    Apple / Android and all platforms also publish a list of the fonts they include. It is a good idea to look thru that list.
     
    Baydogan likes this.
  3. Ivan_K_

    Ivan_K_

    Joined:
    Jun 3, 2019
    Posts:
    4
    Hello,
    I also trying to use OS Font with TMP.

    I tried the simpliest way:
    Code (CSharp):
    1. var font = Font.CreateDynamicFontFromOSFont(new[] {"times"}, 24);
    2. var tmpFontAsset = TMP_FontAsset.CreateFontAsset(font);
    but seems it doesn't work, in console i see such message when run this code:
    Is there any way to create TMP_FontAsset from system font?
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Use the following instead.

    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.  
     
  5. hippogames

    hippogames

    Joined:
    Feb 5, 2015
    Posts:
    228
    It's unclear to put a path as Font constructor accepts 'string name' parameter. But it works somehow.