Search Unity

TextMesh Pro How do I use default system font?

Discussion in 'UGUI & TextMesh Pro' started by NonSpicyBurrito, Apr 4, 2022.

  1. NonSpicyBurrito

    NonSpicyBurrito

    Joined:
    Apr 4, 2020
    Posts:
    14
    My game targets mobile (iOS and Android) and I'm in the process of migrating from legacy UGUI Text to TMP.
    However my game has a huge amount of user generated content which includes CJK characters; a big part of my game also works more like a browser where you browse user generated content (think a forum/social media), and I would like to use the default system font (Android user can even change this themselves) rather than forcing a specific font style.
    In UGUI Text it's as simple as just setting the font face to Arial, and it will pull the default system font at runtime no matter what. I could not find any information regarding how to do this in TMP.
    Dynamic OS font asset apparently requires a reference font which it finds the closes font at runtime (but that's not what I want! I don't want closest matching family, I just want the default); Font.GetPathsToOSFonts doesn't tell you which one is the default; TMP_FontAsset.CreateFontAsset wants you to specific a family. I've looked everywhere and could not find a satisfying answer.
    I understand that a lot of these features are designed with a traditional game in mind, where you want font to match rest of your game UI style, but in my case I do not care and want my game to use the default system font so that users are comfortable reading content with their preferred system font.
    Any help is appreciated.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    The Legacy text system uses an internal hardcoded list of potential fonts to use on different platforms. Although this magic functionality may appear convenient to users at first, it provides no control over font what font is used and on which platform.

    Dynamic OS font assets do require that you import the font you are expecting to use on the target platform. Although this is an extra step in theory, this is something that you should do from a quality assurance point of view as it will enable you to see exactly what you will get on the target platform. In the Editor, the import reference font will be used as a source. In a build, this reference font will not be included. At runtime on the target platform we will search for a font file with the same family name and style name. This is not closest but the same font file.

    For example, if I import NotoSansCJK - Regular in my project and create a dynamic OS font asset from it using the "Noto Sans CJK JP - Regular" face as seen below. Note this is a TrueType Collections (TTC) which contains multiple faces.

    upload_2022-4-4_13-53-56.png

    Like I explained above, when I create a build for Android for instance, the source font file will not be included. The dynamic os font asset will be included. When trying to add characters dynamically at runtime on the target platform (Android), TMP will search for this exact font face which is Noto Sans CJK JP - Regular. Again not the closest or something else but that exact font file (if it exists). This of course requires that you know what fonts are available on the various platforms you are targeting. This information is published by the various manufacturers. Plus you can also test it yourself using the Font.GetPathsToOSFonts()

    In order to target multiple platforms (iOS and Android) for instance, you would simply import NotoSansCJK - Regular which is the default on Android and PingFang - Regular which is the default on iOS. You would assign both of these as local fallback to your primary font asset. Then on Android for example, since NotoSansCJK - Regular is present, it would use that font asset as fallback. On iOS since NotoSansCJK - Regular is not present, it would use PingFang.

    As per the above, you have complete control over what font will be used. Once you know what font you want to use, this take less than a minute to setup. The steps are (1) find the font. (2) import the font. (3) create font asset and set as dynamic OS. (4) assign as fallback to your primary. That is it.

    P.S. I you have not already done so, take the time to watch the following two videos as although they do not specifically cover Dynamic OS font assets, they do cover dynamic font assets (which behave the same way in the editor). These videos also cover localization and the fallback system which is used by Dynamic OS font assets.



     
    Last edited: Apr 4, 2022
  3. NonSpicyBurrito

    NonSpicyBurrito

    Joined:
    Apr 4, 2020
    Posts:
    14
    Appreciate the reply, but I'm not looking to control exactly what font to use, I WANT it to use the default system font because my game is half a "browser," and user expects the game UI to use their system font.
    On Android for example, user can modify their system font to something completely different for ease of reading, and I want my game to use that, instead of finding the exact Noto CJK. This works with the legacy UGUI Text, and I simply want to achieve the same in TMP.
     
    Ryuuguu likes this.
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    I will need to verify if the Legacy text system does in fact change font selection based on what default is set on that platform. As far as I know the list of fonts and their order is hardcoded but I'll will double check.
     
    NonSpicyBurrito likes this.
  5. NonSpicyBurrito

    NonSpicyBurrito

    Joined:
    Apr 4, 2020
    Posts:
    14
    Any update?
     
  6. mdsitton

    mdsitton

    Joined:
    Jan 27, 2018
    Posts:
    66
    So in my testing on windows in unity 2021.3.10f1 with TMP 3.2.0-pre.3 the source font files attached to TMP Assets marked as Dynamic OS do indeed still get included with builds.

    upload_2022-9-20_22-37-55.png

    First build is with OS Fallback fonts configured on windows with the following fonts:
    upload_2022-9-20_22-39-34.png

    Overall if this is the case this basically makes the OS Fallback support completely useless in it's current state since for me the main usecase here is to not have 170mb of CJK fonts shipped with our game build taking up 1/3rd of our game file size.
     
    Last edited: Sep 21, 2022
  7. mdsitton

    mdsitton

    Joined:
    Jan 27, 2018
    Posts:
    66
    Here's a screenshot of one of my TMP_Font Assets as well: upload_2022-9-20_23-13-33.png
     
  8. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    They should not get included but you are right that there might be an issue here. I'll try to test this over the next day or so.

    BTW: Assuming the source font file is present on your system, deleting the source font file from the project would still enable the font asset to work in the Editor. This is not the intended workflow but that would most certainly preclude the source font file from being included in the build.

    P.S. If you were to switch the Inspector to Debug mode, do you still see a reference to the source font file?

    As seen below, you should still have an Editor Ref to the source font file but no other reference should be present for Dynamic OS font assets.

    upload_2022-9-22_2-20-49.png
     
    Last edited: Sep 22, 2022
  9. mdsitton

    mdsitton

    Joined:
    Jan 27, 2018
    Posts:
    66
    So the assets do only seem to have the editor ref and the Source Font FIle on all of them are set to none
     
  10. mdsitton

    mdsitton

    Joined:
    Jan 27, 2018
    Posts:
    66
    Any update?
     
  11. hippogames

    hippogames

    Joined:
    Feb 5, 2015
    Posts:
    234
    Stephan_B, hi! What will be the best approach if we have to deal with international user input? Let's say we're making a social community or a chat system. Currently I had to download 'Arial Unicode MS Regular' at runtime (as I don't want to include it in builds) and create TMP_FontAsset from it. The font is huge and I don't have a license, so it's just for test purposes. I also considered a Unicode font called 'Code2000', it's much lighweight and has unrestricted license.
    Overall, I don't understand how to deal with random texts on different platforms (Android, iOS, Windows) except having such uber-fonts.

    It would be super cool to create TMP_FontAsset from the default Unity's 'Arial' legacy font, but it's not possible at the moment, as it doesn't "Include Font Data". And I actally don't want to know how all the magic inside the legacy font system works, I mean how you're resolving Arial on different platforms. But once you've did this, it should be possible to create a TMP_FontAsset from it at runtime. Sorry if I'm dumb ass)
     
  12. Y-Boy

    Y-Boy

    Joined:
    Jun 2, 2014
    Posts:
    4
    upload_2023-6-25_23-1-27.png

    This setting controls the packaging of the font when used with Dynamic font property. When selected the TTF is included in the output of the build. When not selected it is assumed that the end user will have the font already installed on their machine.
     
  13. Personuo

    Personuo

    Joined:
    Mar 19, 2014
    Posts:
    129
    Any update?
     
  14. Personuo

    Personuo

    Joined:
    Mar 19, 2014
    Posts:
    129
    I never found any option "Dynamic OS", just "Static" and "Dynamic"