[Unity工具]批量修改字体

效果图:

  1 using System.IO;
  2 using System.Text;
  3 using UnityEditor;
  4 using UnityEngine;
  5 using UnityEngine.UI;
  6 
  7 /// <summary>
  8 /// 将目标文件夹下所有Prefab的丢失、默认字体的位置输出,并替换成目标字体
  9 /// </summary>
 10 public class BatchModifyFontWindow : EditorWindow {
 11 
 12     Font toFont = new Font("Arial");//目标字体
 13     string searchFolder = "Assets/Resources/Prefabs";//目标文件夹
 14     string outputPath = Application.dataPath + "/BatchModifyFont.txt";//输出文件路径
 15     bool needReplace = false;//是否要进行替换
 16 
 17     [MenuItem("Window/Custom/BatchModifyFontWindow")]
 18     private static void ShowWindow()
 19     {
 20         EditorWindow.GetWindow<BatchModifyFontWindow>(true, "批量修改字体", true);
 21     }
 22 
 23     void OnGUI()
 24     {
 25         EditorGUILayout.LabelField("功能描述:将目标文件夹下所有Prefab的丢失、默认字体的位置输出,并替换成目标字体");
 26 
 27         GUILayout.BeginHorizontal();
 28         EditorGUILayout.LabelField("目标文件夹为:" + searchFolder);
 29         if (GUILayout.Button("选择文件夹"))
 30         {
 31             string temp = EditorUtility.OpenFolderPanel("请选择目标文件夹", Application.dataPath, "");
 32             if (!string.IsNullOrEmpty(temp))
 33             {
 34                 temp = temp.Substring(temp.IndexOf("Assets"));
 35                 searchFolder = temp;
 36             }
 37         }
 38         GUILayout.EndHorizontal();
 39 
 40         EditorGUILayout.LabelField("输出文件路径:" + outputPath);
 41 
 42         GUILayout.BeginHorizontal();
 43         GUILayout.Label("目标字体为:");
 44         toFont = (Font)EditorGUILayout.ObjectField(toFont, typeof(Font), true, GUILayout.MinWidth(100f));
 45         GUILayout.EndHorizontal();
 46 
 47         GUILayout.BeginHorizontal();
 48         string replaceBtnStr;
 49         if (needReplace)
 50         {
 51             replaceBtnStr = "输出要替换的位置并替换字体";
 52         }
 53         else
 54         {
 55             replaceBtnStr = "输出要替换的位置";
 56         }
 57         if (GUILayout.Button(replaceBtnStr))
 58         {
 59             StringBuilder builder = new StringBuilder();
 60             int counter = 0;
 61             string[] guids = AssetDatabase.FindAssets("t:Prefab", new string[] { searchFolder });
 62             for (int i = 0; i < guids.Length; i++)
 63             {
 64                 string path = AssetDatabase.GUIDToAssetPath(guids[i]);
 65                 //Debug.Log(path);
 66                 EditorUtility.DisplayProgressBar("Hold on", path, (float)(i + 1) / guids.Length);
 67 
 68                 bool hasModifyFont = false;
 69                 GameObject go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
 70                 Text[] texts = go.GetComponentsInChildren<Text>(true);
 71                 for (int j = 0; j < texts.Length; j++)
 72                 {
 73                     Text text = texts[j];
 74                     string fontName;
 75                     string log;
 76                     if (text.font)
 77                     {
 78                         fontName = text.font.name;
 79                     }
 80                     else
 81                     {
 82                         fontName = "Missing";
 83                     }
 84 
 85                     log = path.Substring(0, path.LastIndexOf("/")) + "/" + GetFontPath(go.transform, text.transform) + fontName;
 86                     //Debug.Log(log);
 87 
 88                     //对丢失、默认字体进行处理
 89                     if ((fontName == "Missing") || (fontName == "Arial"))
 90                     {
 91                         if (needReplace)
 92                         {
 93                             text.font = toFont;
 94                             hasModifyFont = true;
 95                         }
 96                         counter++;
 97                         builder.Append(log + "\r\n");
 98                         Debug.Log(log);
 99                     }
100                 }
101 
102                 if (hasModifyFont)
103                 {
104                     EditorUtility.SetDirty(go);
105                 }
106             }
107 
108             if (needReplace)
109             {
110                 Debug.Log(string.Format("替换完成。替换了{0}处", counter));
111             }
112             else
113             {
114                 Debug.Log(string.Format("需要替换{0}处", counter));
115             }
116             File.WriteAllText(outputPath, builder.ToString());            
117             AssetDatabase.Refresh();
118             EditorUtility.ClearProgressBar();
119         }
120         EditorGUILayout.LabelField("替换字体");
121         needReplace = EditorGUILayout.Toggle(needReplace);
122         GUILayout.EndHorizontal();
123     }
124 
125     string GetFontPath(Transform parentTra, Transform fontTra)
126     {
127         string path = "";
128         while (parentTra != fontTra)
129         {
130             path = fontTra.gameObject.name + "/" + path;
131             fontTra = fontTra.parent;
132         }
133         return parentTra.gameObject.name + "/" + path;
134     }
135 }
原文地址:https://www.cnblogs.com/lyh916/p/8034995.html