Search Unity

Is there any way to replace Font A for Font B in Textmesh pro without going thru all of the texts?

Discussion in 'UGUI & TextMesh Pro' started by esteban16108, Nov 5, 2019.

  1. esteban16108

    esteban16108

    Joined:
    Jan 23, 2014
    Posts:
    159
    As the title says.

    Is there any way to replace Font A for Font B in Textmesh pro without going thru all of the texts?

    Happen to start with one font then I discovered the font I really need... and it's a lot of TMP_Text around.

    Thanks!
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    By all the text, do you mean all text objects in a scene or prefabs in a project?
     
    awsapps likes this.
  3. esteban16108

    esteban16108

    Joined:
    Jan 23, 2014
    Posts:
    159
    @Stephan_B Well usually in a scene. I'm thinking if there is a tool like the one used to migrate from the old Textmesh Pro to the new one included in Unity.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    So an Editor tool that would manage font asset assignment on text objects referenced in scenes files potentially?

    Can you provide more insight on your use case?
     
  5. esteban16108

    esteban16108

    Joined:
    Jan 23, 2014
    Posts:
    159
    @Stephan_B Well I think is simple, I worked on a UI with some font using Textmesh Pro to later find out that there was other font that fits more with the theme... so I have to go over all the scene TMP texts and change the Font by hand... About the tool I remember when migrating from the old TMP there is this tool called

    "Project Files GUID Remaping Tool" that will scan all assets using TMP and updating to use the latest version.
    So I thought maybe something on that line?
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    Thank you for providing this additional insight.

    Unfortunately, there is no such tool currently available.

    Now depending on the number of scenes / text objects you would have to update, it might be worth creating such a tool.

    I would suggest taking a look at the TMP_PackageUtilities.cs source code which contains most of the functionality you would need.

    The steps would be roughly as follows:
    (1) Scan the project to find scene files.
    (2) Get the GUID of the current font asset and its replacement.
    (3) Read the scene file to find and replace references of the GUID of the current font asset by its replacement. This is what the line looks like in the Scene file.
    m_fontAsset: {fileID: 11400000, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}
    (4) Now if the text object is using material presets, that would add another layer of complexity as you would need to also replace that as well. This is what the material line looks like
    m_sharedMaterial: {fileID: 2180264, guid: 8f586378b4e144a9851e7b34d9b748ee, type: 2}

    Notice how they are using the same guid. This is because this material preset is actually a sub object of the font asset. Now if this was a separate material, it would have a different guid. You will need to look at a few scene files to figure out how best to handle the potential material preset swap.

    P.S. If I had the time, I would implement this but I am far too busy. If this is something you don't have time or desire to implement, you might be able to get someone from the community here to implement it as this is likely something that would be of value to other TMP users.
     
    esteban16108 likes this.
  7. jcuriel_glu

    jcuriel_glu

    Joined:
    May 26, 2017
    Posts:
    16
    Not sure if anyone still needs this, but I made something quick by duplicating and modifying the code in the TMP_PackageUtilities.cs that @Stephan_B mentioned. It won't handle material presets just looks for the guid of the font asset and replaces it with which ever one you select. Put this script in an editor folder and it should show up in the Window --> TextMeshPro --> Project Font Replacement Tool.
     

    Attached Files:

    Ryuuguu, GosuGame, zhangqj622 and 4 others like this.
  8. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Thanks for posting this, it does a great job and I've found it very useful :)
     
    jcuriel_glu likes this.
  9. kgaloo141

    kgaloo141

    Joined:
    Feb 3, 2022
    Posts:
    1
    It does not work if the object is not a prefab and is in hierarchy can you do something @jcuriel_glu
     
  10. GTGD

    GTGD

    Joined:
    Feb 7, 2012
    Posts:
    436
    Thank you so much for your awesome script. Saved me a lot of tedious work in trying out different fonts across an entire project. Seriously, I hope the Unity Team adds your script to Unity itself.
     
  11. Bonnate

    Bonnate

    Joined:
    May 30, 2021
    Posts:
    5
    Use this if you are simply going to replace the font of the TMP placed in the scene.

    here's super simple and amazing script. Simply put the script in the inspector, specify the font you want, and press the button.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. #if UNITY_EDITOR
    7. using UnityEditor;
    8. #endif
    9.  
    10. /*
    11. https://bonnate.tistory.com/
    12.  
    13. Insert the script into the game object
    14. insert the TMP font in the inspector
    15. and press the button to find and replace all components.
    16.  
    17. It may work abnormally, so make sure to back up your scene before using it!!
    18. */
    19.  
    20. public class TMP_FontChanger : MonoBehaviour
    21. {
    22.     [SerializeField] public TMP_FontAsset FontAsset;
    23. }
    24.  
    25. #if UNITY_EDITOR
    26. [CustomEditor(typeof(TMP_FontChanger))]
    27. public class TMP_FontChangerEditor : Editor
    28. {
    29.     public override void OnInspectorGUI()
    30.     {
    31.         base.OnInspectorGUI();
    32.  
    33.         if (GUILayout.Button("Change Font!"))
    34.         {
    35.             TMP_FontAsset fontAsset = ((TMP_FontChanger)target).FontAsset;
    36.  
    37.             foreach(TextMeshPro textMeshPro3D in GameObject.FindObjectsOfType<TextMeshPro>())
    38.             {
    39.                 textMeshPro3D.font = fontAsset;
    40.             }
    41.             foreach(TextMeshProUGUI textMeshProUi in GameObject.FindObjectsOfType<TextMeshProUGUI>())
    42.             {
    43.                 textMeshProUi.font = fontAsset;
    44.             }
    45.         }
    46.     }
    47. }
    48. #endif
     

    Attached Files:

    Last edited: Jan 11, 2023