Search Unity

TextMesh Pro Generate font asset via script?

Discussion in 'UGUI & TextMesh Pro' started by doraemon213, Feb 13, 2021.

  1. doraemon213

    doraemon213

    Joined:
    Feb 10, 2013
    Posts:
    5
    Is it possible to generate font asset via script?

    I can't access FontEngine, so I don't see a way to generate font without going through the creator GUI window. This is an important feature for us to generate fonts through build machines and save manual labor.

    Am I missing something?
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    You can create font assets by using the TMP_FontAsset.CreateFontAsset() function and then use functions like
    TryAddCharacters() to populate it.

    Once created, you would need to turn it into a persistent asset by using the relevant AssetDatabase functions. You would need to also be certain to add the child material and texture(s) as sub objects like the Font Asset Creator does.

    I would suggest taking a look at the Font Asset Creator code as it contains / does most of what you would need to do. I know several users have implemented their own font asset creation automation in similar fashions.
     
    lvvova and viesc123 like this.
  3. Mr-C

    Mr-C

    Joined:
    Nov 18, 2015
    Posts:
    3
    Finaly, I achieved this by apply some changes to TMPro_FontAssetCreatorWindow class, so that I can call generate method in CI build pipeline. If some one has more robust way, please let me konw, thanks! Example code:
    Code (CSharp):
    1.         private static void DoGenerate(string json, int index, string savePath)
    2.         {
    3.             EditorPrefs.SetString(UFO_TMPro_FontAssetCreatorWindow.k_FontAssetCreationSettingsContainerKey, json);
    4.             EditorPrefs.SetInt(UFO_TMPro_FontAssetCreatorWindow.k_FontAssetCreationSettingsCurrentIndexKey, index);
    5.  
    6.             UFO_TMPro_FontAssetCreatorWindow generator = new UFO_TMPro_FontAssetCreatorWindow();
    7.             generator.OnEnable();
    8.             generator.DoGenerate();
    9.  
    10.             var suc = true;
    11.             var startTime = DateTime.Now;
    12.             while (generator.IsProcessing)
    13.             {
    14.                 generator.Update();
    15.  
    16.                 var newTime = DateTime.Now;
    17.                 var span = newTime - startTime;
    18.                 if (span.TotalSeconds > 60)
    19.                 {
    20.                     Debug.LogError("Generate font asset timeout!");
    21.                     suc = false;
    22.                     break;
    23.                 }
    24.             }
    25.  
    26.             if (suc)
    27.             {
    28.                 generator.DoSave(savePath);
    29.             }
    30.  
    31.             generator.OnDisable();
    32.         }
     
    halley likes this.