Search Unity

TextMesh Pro Changing the missing glyph character with code

Discussion in 'UGUI & TextMesh Pro' started by rempelj, Sep 29, 2018.

  1. rempelj

    rempelj

    Joined:
    Aug 3, 2013
    Posts:
    54
    Hi,

    How can I change the missing glyph character through code, without manually modifying the TMP Settings asset in the inspector? I would like to change the character depending on the build platform and/or region.
     
  2. rempelj

    rempelj

    Joined:
    Aug 3, 2013
    Posts:
    54
    I ended up using reflection to resolve this:

    Code (CSharp):
    1. [MenuItem("Build/Setup TMP Missing Glypth For Current Platform")]
    2. public static void SetupTMPMissingGlypthForCurrentPlatform()
    3. {
    4.     const int MISSING_CHAR_RECTANGLE = 0;
    5.     const int MISSING_CHAR_QUESTION_MARK = 63;
    6.  
    7.     // Get field with reflection
    8.     var settings = UnityEditor.AssetDatabase.LoadAssetAtPath<TMPro.TMP_Settings>("Assets/TextMesh Pro/Resources/TMP Settings.asset");
    9.     var field = typeof(TMPro.TMP_Settings).GetField("m_missingGlyphCharacter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    10.  
    11.     // Set missing char for current platform
    12. #if UNITY_STANDALONE
    13.     field.SetValue(settings, MISSING_CHAR_QUESTION_MARK);
    14. #else
    15.     field.SetValue(settings, MISSING_CHAR_RECTANGLE);
    16. #endif
    17.  
    18.     // Save changes
    19.     UnityEditor.AssetDatabase.Refresh();
    20.     EditorUtility.SetDirty(settings);
    21.     AssetDatabase.SaveAssets();
    22. }
     
  3. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I added a setter on this property to allow you to control this more easily.

    P.S. When I have time, I will do the same for all other properties (where it makes sense) to allow users to change those via scripting.
     
    rempelj likes this.