多方式批量修改Font属性
发表于2018-10-11
之前写过一个 替换场景、Prefab字体 工具类,用于批量修改Font字体,不过只能一键修改全部字体,使用不方便,现在根据刚写过的 多方式批量修改Tag值 工具类,对Font重新写了一个操作窗口,用起来就方便多了,可以根据需求继续扩展~~
#if UNITY_EDITOR using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using UnityEngine.UI; namespace SimpleFrame.Tool { //关于Font的所有属性,均可再次获取并修改,目前只有font、fontSize、fontStyle属性,可根据实际需求再添加 public class ChangeFontByObjWindow : EditorWindow { [MenuItem("MyTools/Font/Change Font By GameObj")] public static void ShowWindow() { //弹出窗口 EditorWindow.GetWindow(typeof(ChangeFontByObjWindow), false, "Change Font Window"); } string showNotify; //目标字体、类型 Font targetFont; int targetFontSize; FontStyle targetFontStyle; List<GameObject> selectObjs = new List<GameObject>(); GameObject newAddObj; void OnEnable() { GameObject[] tmpSelections = Selection.gameObjects; selectObjs.Clear(); selectObjs.AddRange(tmpSelections); targetFontSize = -1; newAddObj = null; showNotify = ""; } void OnGUI() { GUILayout.Space(10); GUILayout.Label("Change Font By GameObj"); GUILayout.Label("U Can Select GameObjects By Mouse In Hierachy And Project Then Open This Window"); //选择目标字体 GUILayout.Space(10); GUILayout.Label("Target Font"); targetFont = (Font)EditorGUILayout.ObjectField(targetFont, typeof(Font), true); //选择目标字号 GUILayout.Label("Target Font Size (if value < 0, will not change font size)"); targetFontSize = EditorGUILayout.IntField(targetFontSize); //选择目标字体类型 GUILayout.Label("Target FontStyle"); targetFontStyle = (FontStyle)EditorGUILayout.EnumPopup(targetFontStyle); //已选中GameObject列表 GUILayout.Space(10); GUILayout.Label("Selection"); if (selectObjs != null && selectObjs.Count > 0) { for (int i = 0; i < selectObjs.Count; i++) { selectObjs[i] = EditorGUILayout.ObjectField(selectObjs[i], typeof(GameObject), true) as GameObject; } } else GUILayout.Label("None"); //新添加到GameObject列表 GUILayout.Space(10); GUILayout.Label("Add To Selection"); newAddObj = EditorGUILayout.ObjectField(newAddObj, typeof(GameObject), true) as GameObject; if (newAddObj != null && !selectObjs.Contains(newAddObj)) { if (GUILayout.Button("Add Obj")) { for (int i = 0; i < selectObjs.Count; i++) { if (selectObjs[i] == null) { selectObjs[i] = newAddObj; showNotify = "Add Obj To Select List"; return; } } selectObjs.Add(newAddObj); showNotify = "Add Obj To Select List"; } } GUILayout.Space(10); if (GUILayout.Button("Change Selection Font")) { ChangeFont(selectObjs, targetFont, targetFontSize, targetFontStyle); } GUILayout.Space(10); if (GUILayout.Button("Change All Scene Font")) { ChangeFont(GetAllSceneTexts(), targetFont, targetFontSize, targetFontStyle); } if (GUILayout.Button("Change All Prefab Font")) { ChangeFont(GetAllPrefabTexts(), targetFont, targetFontSize, targetFontStyle); } GUILayout.Space(10); //关闭弹窗 if (GUILayout.Button("Close")) { this.Close(); } GUILayout.Space(10); EditorGUILayout.TextArea(showNotify, GUILayout.ExpandHeight(true)); this.Repaint(); } void OnDisable() { selectObjs = null; newAddObj = null; showNotify = ""; } void OnLostFocus() { } void ChangeFont(List<GameObject> objs, Font font, int fontSize, FontStyle fontStyle) { if (font == null) { showNotify = "Target Font Is Null"; return; } string tmpNotify = ""; for (int i = 0; i < objs.Count; i++) { if (objs[i] == null) continue; Text tmpText = objs[i].GetComponent<Text>(); if (tmpText != null) { tmpText.font = font; tmpText.fontStyle = fontStyle; if (fontSize >= 0) tmpText.fontSize = fontSize; } else tmpNotify += "\n Not Text Component : " + objs[i].name; } showNotify = tmpNotify + "\n Change Fond Finish"; } List<GameObject> GetAllSceneTexts() { List<GameObject> allSceneTexts = new List<GameObject>(); //获取场景所有Text组件Obj GameObject[] allSceneObjs = Object.FindObjectsOfType<GameObject>(); if (allSceneObjs != null && allSceneObjs.Length > 0) { for (int i = 0; i < allSceneObjs.Length; i++) { Text tmpText = allSceneObjs[i].GetComponent<Text>(); if (tmpText != null) allSceneTexts.Add(allSceneObjs[i]); } } return allSceneTexts; } List<GameObject> GetAllPrefabTexts() { List<GameObject> allPrefabTexts = new List<GameObject>(); //获取Project所有Text组件Obj //获取Asset文件夹下所有Prefab的GUID string[] ids = AssetDatabase.FindAssets("t:Prefab"); string tmpPath; for (int i = 0; i < ids.Length; i++) { //根据GUID获取路径 tmpPath = AssetDatabase.GUIDToAssetPath(ids[i]); if (!string.IsNullOrEmpty(tmpPath)) { //根据路径获取Prefab(GameObject) GameObject tmpObj = AssetDatabase.LoadAssetAtPath(tmpPath, typeof(GameObject)) as GameObject; if (tmpObj != null) { Text tmpText = tmpObj.GetComponent<Text>(); if (tmpText != null) allPrefabTexts.Add(tmpObj); } } } return allPrefabTexts; } } // --------- public class ChangeFontByFontWindow : EditorWindow { [MenuItem("MyTools/Font/Change Font By Font")] public static void ShowWindow() { //弹出窗口 EditorWindow.GetWindow(typeof(ChangeFontByFontWindow), false, "Change Font Window"); } string showNotify; //目标字体、类型 Font targetFont; int targetFontSize; FontStyle targetFontStyle; Font searchFont; Dictionary<GameObject, Text> allSceneTexts = new Dictionary<GameObject, Text>(); Dictionary<GameObject, Text> allPrefabTexts = new Dictionary<GameObject, Text>(); List<GameObject> searchTexts = new List<GameObject>(); void OnEnable() { GetAllSceneTexts(); GetAllPrefabTexts(); targetFontSize = -1; showNotify = ""; } void OnGUI() { GUILayout.Space(10); GUILayout.Label("Change Font By Font"); //选择查找字体 GUILayout.Space(10); GUILayout.Label("Search Font"); searchFont = (Font)EditorGUILayout.ObjectField(searchFont, typeof(Font), true); //查找字体 if (GUILayout.Button("Search")) { searchTexts.Clear(); foreach (var item in allSceneTexts) { if (item.Value.font == searchFont) searchTexts.Add(item.Key); } foreach (var item in allPrefabTexts) { if (item.Value.font == searchFont) searchTexts.Add(item.Key); } } if (searchTexts.Count > 0) { for (int i = 0; i < searchTexts.Count; i++) { searchTexts[i] = EditorGUILayout.ObjectField(searchTexts[i], typeof(GameObject), true) as GameObject; } } else GUILayout.Label("None"); //选择目标字体 GUILayout.Space(10); GUILayout.Label("Target Font"); targetFont = (Font)EditorGUILayout.ObjectField(targetFont, typeof(Font), true); //选择目标字号 GUILayout.Label("Target Font Size (if value < 0, will not change font size)"); targetFontSize = EditorGUILayout.IntField(targetFontSize); //选择目标字体类型 GUILayout.Label("Target FontStyle"); targetFontStyle = (FontStyle)EditorGUILayout.EnumPopup(targetFontStyle); GUILayout.Space(10); if (GUILayout.Button("Change Selection Font")) { ChangeFont(searchTexts, targetFont, targetFontSize, targetFontStyle); } GUILayout.Space(10); if (GUILayout.Button("Change All Scene Font")) { ChangeFont(allSceneTexts, targetFont, targetFontSize, targetFontStyle); } if (GUILayout.Button("Change All Prefab Font")) { ChangeFont(allPrefabTexts, targetFont, targetFontSize, targetFontStyle); } GUILayout.Space(10); //关闭弹窗 if (GUILayout.Button("Close")) { this.Close(); } GUILayout.Space(10); EditorGUILayout.TextArea(showNotify, GUILayout.ExpandHeight(true)); this.Repaint(); } void OnDisable() { allSceneTexts.Clear(); allPrefabTexts.Clear(); searchTexts.Clear(); showNotify = ""; } void GetAllSceneTexts() { allSceneTexts.Clear(); //获取场景所有Text组件 GameObject[] allSceneObjs = Object.FindObjectsOfType<GameObject>(); if (allSceneObjs != null && allSceneObjs.Length > 0) { for (int i = 0; i < allSceneObjs.Length; i++) { Text tmpText = allSceneObjs[i].GetComponent<Text>(); if (tmpText != null) allSceneTexts.Add(allSceneObjs[i], tmpText); } } } void GetAllPrefabTexts() { allPrefabTexts.Clear(); //获取Project所有Text组件 //获取Asset文件夹下所有Prefab的GUID string[] ids = AssetDatabase.FindAssets("t:Prefab"); string tmpPath; for (int i = 0; i < ids.Length; i++) { //根据GUID获取路径 tmpPath = AssetDatabase.GUIDToAssetPath(ids[i]); if (!string.IsNullOrEmpty(tmpPath)) { //根据路径获取Prefab(GameObject) GameObject tmpObj = AssetDatabase.LoadAssetAtPath(tmpPath, typeof(GameObject)) as GameObject; if (tmpObj != null) { Text tmpText = tmpObj.GetComponent<Text>(); if (tmpText != null) allPrefabTexts.Add(tmpObj, tmpText); } } } } void ChangeFont(List<GameObject> objs, Font font, int fontSize, FontStyle fontStyle) { if (font == null) { showNotify = "Target Font Is Null"; return; } string tmpNotify = ""; for (int i = 0; i < objs.Count; i++) { if (objs[i] == null) continue; Text tmpText = objs[i].GetComponent<Text>(); if (tmpText != null) { tmpText.font = font; tmpText.fontStyle = fontStyle; if (fontSize >= 0) tmpText.fontSize = fontSize; } else tmpNotify += "\n Not Text Component : " + objs[i].name; } showNotify = tmpNotify + "\n Change Fond Finish"; } void ChangeFont(Dictionary<GameObject, Text> objs, Font font, int fontSize, FontStyle fontStyle) { if (font == null) { showNotify = "Target Font Is Null"; return; } string tmpNotify = ""; foreach (var item in objs.Values) { if (item == null) continue; Text tmpText = item.GetComponent<Text>(); if (tmpText != null) { tmpText.font = font; tmpText.fontStyle = fontStyle; if (fontSize >= 0) tmpText.fontSize = fontSize; } else tmpNotify += "\n Not Text Component : " + item.name; } showNotify = tmpNotify + "\n Change Fond Finish"; } } } #endif