Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Font Asset - Copies / Aliases?

Discussion in 'UGUI & TextMesh Pro' started by flashframe, Sep 4, 2019.

  1. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    Hi @Stephan_B !

    Is it possible to have copies of font assets that reference the same texture, but have different Face Info settings?

    Thanks!
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    What is the use case?

    Just trying to get a clear understanding of what properties on the Face Info you are modifying and purpose of it all.
     
    Last edited: Sep 4, 2019
  3. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    Oh ok, is there an alternative to modifying those values?

    We are localising the game. In English we use 4 different font assets for different styles of writing. But for some of the languages we are supporting this is just unrealistic, due to the large amount of glyphs.

    So we want to use the same font asset in Chinese (for example) to replace all 4 English fonts. But we want to adjust the values to match the size / line height etc of each English font as closely as possible.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    By styles, you mean 4 distinct font designs / source font files not just visual treatment which can be accomplished with Material Presets to add an Outline, Shadow, etc.

    So for those languages, you would only use one source font file and font asset and if possible create 4 variant with slightly modified Face Info to try to mirror / blend in nicely with the 4 distinct Latin font assets? These 4 variant would share the same atlas texture. Correct?

    What version of Unity are you currently using and version of TextMesh Pro?

    How much text do you have per language that is already known prior to build time vs text that will come from potential external sources and user input?

    P.S. With the Dynamic SDF system, the amount of potential glyphs for any given language should no longer be an issue.

    Dynamic Font Assets and Fallback now also have their atlas texture at size 0 until you add the first glyph so those don't increase build size anymore unless they already contain some glyphs.

    Having to include those East Asian font files which on their own can be upwards to 60 MB could be an issue as including 4 of those font files would increase the build size by a lot. In the next release for Unity 2018.4 or Newer, it will also be possible to create font assets at runtime using fonts available on local devices. So provided you know which fonts are available on those devices in order to select which ones will work well for your needs, that is another option to get around having to include those larger East Asian font files.

    P.S.S. Still trying to get the full picture and cover existing options before looking into adding a new feature which would take longer to get in your hands.
     
    Last edited: Sep 5, 2019
  5. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    Sorry for not being clear :)

    This is exactly correct.

    By font styles I really mean "fonts". For example we have a typewriter font, a handwriting font and a "normal" font. These fonts are all fundamentally different in appearance. For some languages we have just accepted we can't have all these different fonts, and so we will just replace them all with the same font asset.

    At runtime I'm switching the font asset and material preset if the player changes languages. Where we have used different fonts in English, we need the localised font to match as closely in size and placement. But yes, we'd like to share the same texture per language to save on memory.

    We don't need dynamic font assets in this instance because we know all the glyphs we need already.

    So what I'm asking is can we make duplicates of a font asset, but have them all reference the same texture so that aren't loading in the same texture redundantly multiple times?

    Thanks again!

    Edit: Unity 2018.4 & TMPro 1.4.1
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    In the following location in the Global package cache, "...\AppData\Local\Unity Cache\cache\packages\packages.unity.com\com.unity.textmeshpro@1.4.1\Scripts\Editor" open the following script "TMP_FontAsset_CreationMenu.cs" which is currently read-only so you will have to change that to make the changes. Add the following new option.

    Code (csharp):
    1.  
    2. [MenuItem("Assets/Create/TextMeshPro/Font Asset Variant", false, 105)]
    3. public static void CreateFontAssetVariant()
    4. {
    5.     Object target = Selection.activeObject;
    6.     // Make sure the selection is a font file
    7.     if (target == null || target.GetType() != typeof(TMP_FontAsset))
    8.     {
    9.         Debug.LogWarning("A Font file must first be selected in order to create a Font Asset.");
    10.         return;
    11.     }
    12.     TMP_FontAsset sourceFontAsset = (TMP_FontAsset)target;
    13.     string sourceFontFilePath = AssetDatabase.GetAssetPath(target);
    14.     string folderPath = Path.GetDirectoryName(sourceFontFilePath);
    15.     string assetName = Path.GetFileNameWithoutExtension(sourceFontFilePath);
    16.     string newAssetFilePathWithName = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + assetName + " - Variant.asset");
    17.  
    18.     //// Create new instance of font asset from the source font asset.
    19.     TMP_FontAsset fontAsset = ScriptableObject.Instantiate<TMP_FontAsset>(sourceFontAsset);
    20.     AssetDatabase.CreateAsset(fontAsset, newAssetFilePathWithName);
    21.  
    22.     // Set Texture and Material reference to the source font asset.
    23.     fontAsset.atlasTextures = sourceFontAsset.atlasTextures;
    24.     fontAsset.material = sourceFontAsset.material;
    25.  
    26.     fontAsset.atlasPopulationMode = AtlasPopulationMode.Static;
    27.  
    28.     EditorUtility.SetDirty(fontAsset);
    29.     AssetDatabase.SaveAssets();
    30. }
    31.  
    To use this, select a source / target font asset and use the Context Menu "Create - TextMeshPro - Font Asset Variant". This will create a new persistent font asset from the source.

    Unlike other font assets, this variant won't have any child sub object Texture and Material as it will reference those of the source / target font asset.

    This obviously hasn't been fully tested so please keep that in mind and really test it. It should work fine for static font assets but will have issues when Resetting the parent which would mostly be used if the font asset is dynamic and you wish to reset it. I'll look into improving that later on.

    Right now, if you regenerate the source font asset, the variant will likely lose their reference to its texture and material but you should be able to manually re-assign those or re-create the variants. Again something that I can address later to make this more functional.

    These variant have copies of the parent source data in terms of Face Info, Character, Glyph and Adjustment Pair data. However, this data is unique to the variant so glyph metrics can be edited to have unique scale and values. GlyphRect data should not be edited since that is specific to the source atlas texture.

    Please give this a spin and let me know if you run into any issues. You are the Alpha and Beta tester on this ;)
     
    Last edited: Sep 5, 2019
    flashframe likes this.
  7. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    Thank you! I will try this right now
     
  8. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    Hey Stephan,

    This is exactly what we wanted, thank you! The variant works as expected and stepping through the frame debugger shows they are referencing the same texture (and when sharing a material, also grouped into a single draw call.)

    The only thing I had difficulty with was finding a way to get Unity to reload the package - tried restarting, reimporting a few of my own assets, with no luck. So I ended up adding the Menu Item to my own script to get it to show up. But obviously that's fine.

    We are spending the whole week localising the game, so will be able to test thoroughly. I'll feedback any issues, but seems like it should work well for our use case.

    Thank you so much!
     
    Last edited: Sep 5, 2019
  9. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    Glad it is working.

    Obviously, very little time when into thinking about this feature so I look forward to the feedback.

    I was thinking last night about whether or not we want these variants to have their own material as a sub object just so they can have their own default settings.

    I know you are not using the dynamic system at this time but I will also need to figure out how to make these work with it as these variants would need to be aware of glyphs and characters as well as font features being added / removed from their parent font asset.

    Just stuff to think about...
     
  10. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    798
    Yeah that's probably a good idea. In my case, all the default materials on the English fonts have the same settings, so it's not necessary. So far it's all working as expected.

    I can see how adding anything starts to spiral out as you need to ensure it supports all use cases and features. Thankfully, this initial use case is pretty straightforward, so a good test for the basics. :)
     
  11. unity_MIKfdGOqVLi1xw

    unity_MIKfdGOqVLi1xw

    Joined:
    Nov 22, 2020
    Posts:
    2
    Hello @Stephan_B !

    I'm wondering: what's the current status of the Font Asset Variants? :) I'm on TMP 3.0.6 and I can see the Font Asset Variant in creation menu, but I'm wondering if there was anything done to make it work with dynamic fonts?
     
    Last edited: Mar 1, 2024