Search Unity

[TextMesh Pro] How to switch font files in Dynamic SDF

Discussion in 'UGUI & TextMesh Pro' started by ysalpha, Jul 8, 2021.

  1. ysalpha

    ysalpha

    Joined:
    Feb 2, 2017
    Posts:
    52
    How can I change the Source Font of a Dynamic SDF that has already been generated?
    I have tried the following steps, but they do not work correctly.

    1. From the Dynamic SDF Inspector, click "Update Atlas Texture".
    2. Drop the font file you want to replace into "Source Font File".
    3. Leave the "Character Sequence (Hex)" blank and click "Generate Font Atlas".
    4. Click "Save" to overwrite the Dynamic SDF.asset
    5. Drop the font file you want to replace into the Source Font File in the Generation Settings of the Dynamic SDF Inspector.

    I thought this would work correctly, but the TextMeshPro object that I originally assigned the Dynamic SDF to no longer displays any text.
    Shouldn't the atlas textures be updated with the newly assigned font files?
    I looked at the Dynamic SDF information in the Inspector's "Debug", and it looks like the previously assigned font file information is still there, and this is causing the Atlas texture to not update correctly.

    A sample project is available, let me know if you need it.

    Unity Version: 2020.3.11f1
    TextMeshPro Version: 3.0.6
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Are you trying to combine glyphs from different font files into the same font asset?
     
  3. ysalpha

    ysalpha

    Joined:
    Feb 2, 2017
    Posts:
    52
    No, it's not.
    I don't want to use different font files at the same time.
    I want to REPLACE existing font file with a different one.

    I posted this because I was unable to replace it correctly using the previous steps.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I am able to reproduce the behavior you described above.

    Before making any changes, I would like to get a better understanding of the use case.

    Since the font asset is dynamic, seems like a user should only have to change the "Source Font File" for this to work (which it doesn't right now) that is instead of having to Update Atlas Texture and then going through the Font Asset Creator?

    or is there a reason, you would need to go through the Font Asset Creator (putting aside the issue you reported)?

    Out of curiosity, why change the source font file? The font asset will still have the previous name so that might end up being confusing?
     
  5. ysalpha

    ysalpha

    Joined:
    Feb 2, 2017
    Posts:
    52
    I updated the atlas textures via the font asset creator, thinking that maybe if I reworked the atlas textures, they would replace correctly.
    I also think you are right that there is no need to go through the font asset creator if you can just change the source font file and it will replace correctly.

    Perhaps you mean: "Wouldn't it be less confusing to give the same name to the font assets and the source font files?"
    (i.e., naming it something like NotoSerif-Regular.otf -> NotoSerif-Regular SDF.asset)".

    This naming method has the problem of being difficult to manage. (This is not limited to dynamic fonts, though.)
    The main reason for this is that renaming and re-creating font assets means that we have to reassign SDFs for all TextMeshPro components. This is a very difficult task.
    To avoid this, create a font asset whose name will never change (such as Game SDF.asset), and always assign that SDF during development.
    This way, if you want to change the source font file, you can simply switch the Source Font File that the font asset refers to.

    You may think that the problem of reassignment can be solved by using the Default Font Asset in TMP Settings.
    However, for me personally, The above method of creating is by far the most manageable.
    In the first place, it doesn't feel right to have a font asset attached to a TextMeshPro component with a unique font name like "NotoSerif-Regular SDF". I feel something is not quite right.

    So we want to switch only the source font file with the font assets keeping their previous names.
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I made the relevant changes to make sure that when changing the source font file in the font asset inspector, that the font asset updates correctly as well as any text object using this font asset. This fix / change will be in the next release of the TMP package.

    A text object includes many properties including the text content, the font asset, point size, style, color, etc. Those are sort of core / typical properties that most users would expect on a text object. Furthermore, given a scene may contain many text objects using different fonts and styles, just like being able to specify the text, point size, etc. for a particular object, you have to be able to also specify its font asset.

    Having said that, I did make the fix outlined above to enable you to change the source font file and have it behave as you expect.

    Still, I would like to get a better understanding of your needs. I certainly understand that you have a project that contains multiple text objects where you would like to be able to easily change the font asset used by all those objects. Perhaps you want to do this in the Editor while developing the project to test / experiment with different fonts or perhaps for localization purposes to support multiple languages.

    With respect to localization, it is pretty common for users to want to change the font asset assigned to multiple text objects. It is also common for them to also want to change other properties like the text, point size, color, etc. at the same time as well.

    To achieve this many users use a Localization tool such as I2 Localization or the experimental Unity Localization Tool / Package. These tools, not only control how the text is affected by local changes but also what properties might need to be changed as well.

    Other users have implemented their own manager which tracks in some collection all the text objects so they can easily iterate over this collection to change various properties including the font asset.

    If your needs are not related to localization and you simply want to more easily change the font asset per text object, another alternative would be to define a style that is used by all those text objects where the style itself defines the font asset used by those objects. Styles are defined in a style sheet where you can have both local and global styles. See example 11 - The Style Tag included in the TMP Examples & Extras.
     
  7. ysalpha

    ysalpha

    Joined:
    Feb 2, 2017
    Posts:
    52
    Thanks for the great advice and fixes!
    I now have a better understanding of TextMeshPro.