Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Gametronic · Jun 02, 2016 at 04:05 PM · fonts

How do I change font of all texts in all scenes?

Hi,

I developed around 20 scenes and all are contains some texts. Now I have new fonts that I want to apply to all texts. I don't want to go one by one and do it manually. Is there any way to set it somewhere and all texts automatically use that font?

Please help...

Thanks in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by ThomLaurent · May 31, 2017 at 01:45 PM

Late reply.

Here is a an editor window script I made to Find and Replace every UnityEngine.UI.Text fonts in open scene(s) with the option to include project prefabs. (Tested on Unity 5.6.0f3)

If you don't specify a fond to find it will match them all.

Go to Utilities > Replace Font... to use it!

 using System.Collections.Generic;
 using System.Linq;
 using UnityEditor;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class FontReplacer : EditorWindow
 {
     private const string EditorPrefsKey = "Utilities.FontReplacer";
     private const string MenuItemName = "Utilities/Replace Fonts...";
 
     private Font _src;
     private Font _dest;
     private bool _includePrefabs;
 
     [MenuItem(MenuItemName)]
     public static void DisplayWindow()
     {
         var window = GetWindow<FontReplacer>(true, "Replace Fonts");
         var position = window.position;
         position.size = new Vector2(position.size.x, 151);
         position.center = new Rect(0f, 0f, Screen.currentResolution.width, Screen.currentResolution.height).center;
         window.position = position;
         window.Show();
     }
 
     public void OnEnable()
     {
         var path = EditorPrefs.GetString(EditorPrefsKey + ".src");
         if (path != string.Empty)
             _src = AssetDatabase.LoadAssetAtPath<Font>(path) ?? Resources.GetBuiltinResource<Font>(path);
 
         path = EditorPrefs.GetString(EditorPrefsKey + ".dest");
         if (path != string.Empty)
             _dest = AssetDatabase.LoadAssetAtPath<Font>(path) ?? Resources.GetBuiltinResource<Font>(path);
 
         _includePrefabs = EditorPrefs.GetBool(EditorPrefsKey + ".includePrefabs", false);
     }
 
     public void OnGUI()
     {
         EditorGUI.BeginChangeCheck();
         EditorGUILayout.PrefixLabel("Find:");
         _src = (Font)EditorGUILayout.ObjectField(_src, typeof(Font), false);
 
         EditorGUILayout.Space();
         EditorGUILayout.PrefixLabel("Replace with:");
         _dest = (Font)EditorGUILayout.ObjectField(_dest, typeof(Font), false);
 
         EditorGUILayout.Space();
         _includePrefabs = EditorGUILayout.ToggleLeft("Include Prefabs", _includePrefabs);
         if (EditorGUI.EndChangeCheck())
         {
             EditorPrefs.SetString(EditorPrefsKey + ".src", GetAssetPath(_src, "ttf"));
             EditorPrefs.SetString(EditorPrefsKey + ".dest", GetAssetPath(_dest, "ttf"));
             EditorPrefs.SetBool(EditorPrefsKey + ".includePrefabs", _includePrefabs);
         }
         
         GUI.color = Color.green;
         if (GUILayout.Button("Replace All", GUILayout.Height(EditorGUIUtility.singleLineHeight * 2f)))
         {
             ReplaceFonts(_src, _dest, _includePrefabs);
         }
         GUI.color = Color.white;
     }
 
     private static void ReplaceFonts(Font src, Font dest, bool includePrefabs)
     {
         var sceneMatches = 0;
         for (var i = 0; i < SceneManager.sceneCount; i++)
         {
             var scene = SceneManager.GetSceneAt(i);
             var gos = new List<GameObject>(scene.GetRootGameObjects());
             foreach (var go in gos)
             {
                 sceneMatches += ReplaceFonts(src, dest, go.GetComponentsInChildren<Text>(true));
             }
         }
 
         if (includePrefabs)
         {
             var prefabMatches = 0;
             var prefabs =
                 AssetDatabase.FindAssets("t:Prefab")
                     .Select(guid => AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(guid)));
             foreach (var prefab in prefabs)
             {
                 prefabMatches += ReplaceFonts(src, dest, prefab.GetComponentsInChildren<Text>(true));
             }
 
             Debug.LogFormat("Replaced {0} font(s), {1} in scenes, {2} in prefabs", sceneMatches + prefabMatches, sceneMatches, prefabMatches);
         }
         else
         {
             Debug.LogFormat("Replaced {0} font(s) in scenes", sceneMatches);
         }
     }
 
     private static int ReplaceFonts(Font src, Font dest, IEnumerable<Text> texts)
     {
         var matches = 0;
         var textsFiltered = src != null ? texts.Where(text => text.font == src) : texts;
         foreach (var text in textsFiltered)
         {
             text.font = dest;
             matches++;
         }
         return matches;
     }
 
     private static string GetAssetPath(Object assetObject, string defaultExtension)
     {
         var path = AssetDatabase.GetAssetPath(assetObject);
         if (path.StartsWith("Library/", System.StringComparison.InvariantCultureIgnoreCase))
             path = assetObject.name + "." + defaultExtension;
         return path;
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image domportera · Sep 30, 2021 at 09:48 AM 0
Share

thank you for this!!

avatar image
1

Answer by Mmmpies · Jun 02, 2016 at 04:56 PM

Not sure if there's something in the editor tha can do it but you can swap them out when the game loads with this.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class TextChanger : MonoBehaviour {
 
     private Text[] GetText;
     public Font myFont;
 
     // Use this for initialization
     void Start () {
         GetText = Text.FindObjectsOfType<Text> ();
 
         foreach (Text go in GetText)
             go.font = myFont;
     }
 }

It's not very good practice though so if someone knows an editor trick to change them go with that.

EDIT - forgot to say, you need that script on any gameObject in each and every scene. It runs when the scene starts so if you have a prefab that instantiates later it may not catch that.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by MochiBits · Nov 04, 2018 at 06:04 PM

Thanks for the script.

Note to add:

 UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(scene);

So you can then Save the changes to the scene.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Thrainsa · May 13, 2021 at 10:32 AM

Thanks for the Script, I updated it for TextMeshPro ;)

 using System.Collections.Generic;
 using System.Linq;
 using UnityEditor;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using TMPro;
 
 public class TMProFontReplacer : EditorWindow
 {
     private const string EditorPrefsKey = "Utilities.TMProFontReplacer";
     private const string MenuItemName = "Utilities/Replace TMProFonts...";
 
     private TMP_FontAsset _src;
     private TMP_FontAsset _dest;
     private bool _includePrefabs;
 
     [MenuItem(MenuItemName)]
     public static void DisplayWindow()
     {
         var window = GetWindow<TMProFontReplacer>(true, "Replace TMProFonts");
         var position = window.position;
         position.size = new Vector2(position.size.x, 151);
         position.center = new Rect(0f, 0f, Screen.currentResolution.width, Screen.currentResolution.height).center;
         window.position = position;
         window.Show();
     }
 
     public void OnEnable()
     {
         var path = EditorPrefs.GetString(EditorPrefsKey + ".src");
         if (path != string.Empty)
             _src = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(path) ?? Resources.GetBuiltinResource<TMP_FontAsset>(path);
 
         path = EditorPrefs.GetString(EditorPrefsKey + ".dest");
         if (path != string.Empty)
             _dest = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(path) ?? Resources.GetBuiltinResource<TMP_FontAsset>(path);
 
         _includePrefabs = EditorPrefs.GetBool(EditorPrefsKey + ".includePrefabs", false);
     }
 
     public void OnGUI()
     {
         EditorGUI.BeginChangeCheck();
         EditorGUILayout.PrefixLabel("Find:");
         _src = (TMP_FontAsset)EditorGUILayout.ObjectField(_src, typeof(TMP_FontAsset), false);
 
         EditorGUILayout.Space();
         EditorGUILayout.PrefixLabel("Replace with:");
         _dest = (TMP_FontAsset)EditorGUILayout.ObjectField(_dest, typeof(TMP_FontAsset), false);
 
         EditorGUILayout.Space();
         _includePrefabs = EditorGUILayout.ToggleLeft("Include Prefabs", _includePrefabs);
         if (EditorGUI.EndChangeCheck())
         {
             EditorPrefs.SetString(EditorPrefsKey + ".src", GetAssetPath(_src, "ttf"));
             EditorPrefs.SetString(EditorPrefsKey + ".dest", GetAssetPath(_dest, "ttf"));
             EditorPrefs.SetBool(EditorPrefsKey + ".includePrefabs", _includePrefabs);
         }
 
         GUI.color = Color.green;
         if (GUILayout.Button("Replace All", GUILayout.Height(EditorGUIUtility.singleLineHeight * 2f)))
         {
             ReplaceFonts(_src, _dest, _includePrefabs);
         }
         GUI.color = Color.white;
     }
 
     private static void ReplaceFonts(TMP_FontAsset src, TMP_FontAsset dest, bool includePrefabs)
     {
         var prefabMatches = 0;
         if (includePrefabs)
         {
             var prefabs =
                 AssetDatabase.FindAssets("t:Prefab")
                     .Select(guid => AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(guid)));
             foreach (var prefab in prefabs)
             {
                 prefabMatches += ReplaceFonts(src, dest, prefab.GetComponentsInChildren<TextMeshProUGUI>(true));
                 UnityEditor.EditorUtility.SetDirty(prefab);
             }
         }
 
         var sceneMatches = 0;
         for (var i = 0; i < SceneManager.sceneCount; i++)
         {
             var scene = SceneManager.GetSceneAt(i);
             var gos = new List<GameObject>(scene.GetRootGameObjects());
             foreach (var go in gos)
             {
                 sceneMatches += ReplaceFonts(src, dest, go.GetComponentsInChildren<TextMeshProUGUI>(true));
             }
         }
         UnityEditor.SceneManagement.EditorSceneManager.MarkAllScenesDirty();
 
         if (includePrefabs)
         {
             Debug.LogFormat("Replaced {0} font(s), {1} in scenes, {2} in prefabs", sceneMatches + prefabMatches, sceneMatches, prefabMatches);
         }
         else
         {
             Debug.LogFormat("Replaced {0} font(s) in scenes", sceneMatches);
         }
     }
 
     private static int ReplaceFonts(TMP_FontAsset src, TMP_FontAsset dest, IEnumerable<TextMeshProUGUI> texts)
     {
         var matches = 0;
         var textsFiltered = src != null ? texts.Where(text => text.font == src) : texts;
         foreach (var text in textsFiltered)
         {
             text.font = dest;
             matches++;
         }
         return matches;
     }
 
     private static string GetAssetPath(Object assetObject, string defaultExtension)
     {
         var path = AssetDatabase.GetAssetPath(assetObject);
         if (path.StartsWith("Library/", System.StringComparison.InvariantCultureIgnoreCase))
             path = assetObject.name + "." + defaultExtension;
         return path;
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUIStyle and setting font's 2 Answers

Unity and font licensing 0 Answers

Trouble with fonts. 0 Answers

Use Windows Fonts in Game 1 Answer

Font.CacheFontForText is 87.3% in the profiler and causes a HUGE lag spike 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges