Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

TextMesh Pro Set Font Fallback in runtime without changing TMP_FontAsset asset

Discussion in 'UGUI & TextMesh Pro' started by JasonAppxplore, Jan 20, 2021.

  1. JasonAppxplore

    JasonAppxplore

    Joined:
    Jan 14, 2015
    Posts:
    18
    We're trying to implement localization support for CJK, and these are what we're trying to achieve :

    1. Base Game will contain English/Latin Static Font Asset (Default Language)
    2. Addressable Asset Bundle that contains CJK Static Font Asset that includes all known text in the game (will be downloaded when player switch language)
    3. Base Game will contain CJK Dynamic Font Asset (For handling player inputs, in this case, to display other player's names)

    We're thinking of setting the Dynamic Font Asset as a fallback for the Static Font Asset, to handle any missed texts, and also not requiring to rebuild that asset bundle with every small update. However, if I set that in the editor, the Dynamic Font Asset (together with the .ttf font file) will be packed into the bundle.

    So I tried setting the fallback after the Static Font has loaded. But, this will modify the asset when playing in editor, causing the fallback to be saved.

    Is there any other way I can do this?
     
  2. pfay

    pfay

    Joined:
    Mar 2, 2020
    Posts:
    7
    For me, using an editor-only script to constantly reset the font's fallbacks (when it's not playing) does what I need.

    Code (CSharp):
    1. using TMPro;
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. namespace Studio.Game.Editor
    6. {
    7.     [InitializeOnLoad]
    8.     public class TMPSettingsEnforcer
    9.     {
    10.         static TMPSettingsEnforcer ()
    11.         {
    12.             EditorApplication.update += Update;
    13.         }
    14.        
    15.         private static void Update()
    16.         {
    17.             if (Application.isPlaying)
    18.             {
    19.                 return;
    20.             }
    21.             TMP_Settings.defaultFontAsset.fallbackFontAssetTable.Clear();
    22.         }
    23.        
    24.     }
    25. }