Search Unity

How to automate TextMeshPro 2.0 and up (Font Asset Creator)?

Discussion in 'UGUI & TextMesh Pro' started by philtha, May 2, 2019.

  1. philtha

    philtha

    Joined:
    Mar 14, 2017
    Posts:
    9
    Iam looking for a way to automate lots of Font Assets and updating them from time to time.

    The old way was to use the ForntPlugin

    TMP_FontPlugin.Initialize_FontEngine(); ....and finaly render them into the atlas
    errorCode = TMPro_FontPlugin.Render_Characters(textureBuffer, atlasWidth, atlasHeight, characterPadding, characterArray, characterCount, fontStyle, strokeSize, useAutoSizing, fontRenderMode, (int)fontPackingMode, ref fontFaceInfo, fontGlyphInfo);

    Now it looks like this possibility is gone.

    What is left is the Window
    namespace TMPro.EditorUtilities
    {
    public class TMPro_FontAssetCreatorWindow : EditorWindow

    should we use that for doing automations?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I'll need to make some tweaks / expose a few more API functions to provide this functionality via the new FontEngine API.

    The revised Font Asset Creator has this functionality but it is more granular now. Until I find the time to expose additional API functionality, I suggest you take a look at how the Font Asset Creator is handling this since the source code for the package is available.

    P.S. Currently, it is possible to create font assets at runtime and in the editor. It is also possible to add glyphs and characters to them but the auto-sizing feature isn't exposed in those functions. Basically, I need to add a few overloads to handle this.
     
    ina likes this.
  3. philtha

    philtha

    Joined:
    Mar 14, 2017
    Posts:
    9
    Thanks a lot for the fast reply.
     
  4. NiallSoe

    NiallSoe

    Joined:
    Jan 30, 2018
    Posts:
    17
    Hi @Stephan_B,

    Hope you're doing OK in these crazy times.

    Sorry to necro this thread almost a year after the last activity, but I am also looking to automate font asset creation in the editor.

    I have attempted to replicate the "Generate Font Atlas" code from TMPro_FontAssetCreatorWindow in my own Editor scripts but have run into some issues where required functionality is marked as internal.

    Can the following be made public, please?
    1. GlyphRasterModes enum
    2. FontEngine.TryPackGlyphsInAtlas
    3. FontEngine.RenderGlyphsToTexture
    I am currently using v1.4.1 from the Package Manager, but looking at the release notes the above changes do not seem to have already been made in any of the 1.5 previews.

    Thanks a lot,

    Niall
     
    freakrho, ina and jakovd like this.
  5. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,085
    any update??
     
    lucast_unity likes this.
  6. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,085
    what's the latest?
     
  7. freakrho

    freakrho

    Joined:
    Aug 19, 2013
    Posts:
    9
    I managed to recreate the font atlas creation using reflection
    Here's the class I created: https://gitlab.com/-/snippets/2077829
    You have to set the parameters using reflection as well, here's an example:

    Code (CSharp):
    1.             typeof(TMPro_FontAssetCreatorWindow)
    2.                 .GetField("m_SourceFontFile", BindingFlags.NonPublic | BindingFlags.Instance)
    3.                 .SetValue(window, AssetDatabase.LoadAssetAtPath<UnityEngine.Object>(font));
    4.             typeof(TMPro_FontAssetCreatorWindow)
    5.                 .GetField("m_CharacterSetSelectionMode", BindingFlags.NonPublic | BindingFlags.Instance)
    6.                 .SetValue(window, 8);
    7.             typeof(TMPro_FontAssetCreatorWindow)
    8.                 .GetField("m_CharactersFromFile", BindingFlags.NonPublic | BindingFlags.Instance)
    9.                 .SetValue(window, AssetDatabase.LoadAssetAtPath<TextAsset>(path));
    10.             typeof(TMPro_FontAssetCreatorWindow)
    11.                 .GetField("m_PackingMode", BindingFlags.NonPublic | BindingFlags.Instance)
    12.                 .SetValue(window, 4);
    13.             typeof(TMPro_FontAssetCreatorWindow)
    14.                 .GetField("m_AtlasWidth", BindingFlags.NonPublic | BindingFlags.Instance)
    15.                 .SetValue(window, set.AtlasWidth);
    16.             typeof(TMPro_FontAssetCreatorWindow)
    17.                 .GetField("m_AtlasHeight", BindingFlags.NonPublic | BindingFlags.Instance)
    18.                 .SetValue(window, set.AtlasHeight);
    19.             typeof(TMPro_FontAssetCreatorWindow)
    20.                 .GetField("m_IncludeFontFeatures", BindingFlags.NonPublic | BindingFlags.Instance)
    21.                 .SetValue(window, true);
    22.  
    I might do a proper asset in the future
     
    t1ny_bear and cellarmation like this.