[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 }
相關文章
相關標籤/搜索