1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
| using System.Collections.Generic; using System.IO; using TMPro; using UnityEditor; using UnityEngine; using UnityEngine.UI; using System.Linq; using System;
public class ClearPrefabTextContent : EditorWindow { private static List<KeyValuePair<string, string>> textDict; private Vector2 scrollPosition; private static string folderPath, defaultPath = "Assets/_GameCenter/_Resources/prefab"; private static string cnt = "";
[MenuItem("Tools/文本组件管理")] private static void TextComponentManager() { ShowWindow(); }
public static void ShowWindow() { GetWindow<ClearPrefabTextContent>("Prefab Text Content Manager"); }
private void OnGUI() { GUILayout.Label("Prefab Text Content Manager Tool", EditorStyles.boldLabel); EditorGUILayout.Space(); folderPath = string.IsNullOrEmpty(folderPath) ? defaultPath : folderPath; EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Prefab Folder Path:", folderPath); if (GUILayout.Button("...", GUILayout.Width(30))) { var path = EditorUtility.OpenFolderPanel("Select Prefab Folder", defaultPath, ""); if (string.IsNullOrEmpty(path)) return; if (path.StartsWith(Application.dataPath)) folderPath = path.Substring(path.IndexOf("Assets")); } EditorGUILayout.EndHorizontal(); EditorGUILayout.Space(); if (GUILayout.Button("查询非空文本控件")) { cnt = ""; if (string.IsNullOrEmpty(folderPath) || !Directory.Exists(folderPath)) cnt += "Please select a valid folder path first!"; else FindNotEmptyText(); } EditorGUILayout.Space(); if (GUILayout.Button("清空文本控件")) { cnt = ""; if (string.IsNullOrEmpty(folderPath) || !Directory.Exists(folderPath)) cnt += "Please select a valid folder path first!"; else ClearText(); } EditorGUILayout.Space(); GUILayout.Label("查询结果:", EditorStyles.boldLabel); scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.ExpandHeight(true)); EditorGUILayout.TextArea(cnt, GUILayout.ExpandHeight(true)); EditorGUILayout.EndScrollView(); }
private static void FindNotEmptyText() { textDict = new List<KeyValuePair<string, string>>(); var path = folderPath; cnt += string.Format("开始查询 {0} 下所有的预制体\n", path); if (!path.Contains("Assets")) { cnt += "所选文件夹不在 Assets 目录下,无法处理!"; return; } path = path.Substring(path.IndexOf("Assets")); var guids = AssetDatabase.FindAssets("t:Prefab", new string[] { path }); var prefabCount = 0; var textCount = 0; if (guids != null && guids.Length > 0) { EditorUtility.DisplayProgressBar("Finding...", "Start", 0); var progress = 0; var lastPrefab = ""; foreach (var guid in guids) { progress++; var assetsPath = AssetDatabase.GUIDToAssetPath(guid); EditorUtility.DisplayProgressBar("Checking....", assetsPath, (float)progress / guids.Length); var obj = AssetDatabase.LoadAssetAtPath(assetsPath, typeof(GameObject)) as GameObject; var lblist1 = obj.GetComponentsInChildren<Text>(true); var lblist2 = obj.GetComponentsInChildren<RichText>(true); var lblist3 = obj.GetComponentsInChildren<TMP_Text>(true); var allTextComponents = new List<Component>() .Concat(lblist1) .Concat(lblist2) .Concat(lblist3) .ToList(); foreach (var item in allTextComponents) { var text = ""; if (item is Text uguiText) text = uguiText.text; else if (item is RichText richText) text = richText.text; else if (item is TMP_Text tmpText) text = tmpText.text; if (lastPrefab != guid && text != "") { prefabCount++; cnt += string.Format("\n{0}预制体:{1} ", prefabCount, obj.name); lastPrefab = guid; } if (text != "") { textCount++; cnt += string.Format("\n {2}文本路径:{3}/{0} 文本内容:{1}", item.name, text, textCount, item.transform.parent.name); textDict.Add(new KeyValuePair<string, string>(assetsPath, text)); } } } AssetDatabase.SaveAssets(); EditorUtility.ClearProgressBar(); } cnt += "\n\n查询完成"; }
private static void ClearText() { var folder = folderPath; cnt += string.Format("开始查询 {0} 下所有的预制体\n", folder); if (!folder.Contains("Assets")) { cnt += "所选文件夹不在 Assets 目录下,无法处理!"; return; } folder = folder.Substring(folder.IndexOf("Assets")); var guids = AssetDatabase.FindAssets("t:Prefab", new string[] { folder }); if (guids != null && guids.Length > 0) { EditorUtility.DisplayProgressBar("Clean...", "Start", 0); var progress = 0; foreach (var guid in guids) { progress++; var path = AssetDatabase.GUIDToAssetPath(guid); EditorUtility.DisplayProgressBar("Replace....", path, (float)progress / guids.Length); var prefab = PrefabUtility.LoadPrefabContents(path); var textComponentsDict = new Dictionary<Type, IEnumerable<Component>> { { typeof(Text), prefab.GetComponentsInChildren<Text>(true) }, { typeof(RichText), prefab.GetComponentsInChildren<RichText>(true) }, { typeof(TMP_Text), prefab.GetComponentsInChildren<TMP_Text>(true) } }; foreach (var kvp in textComponentsDict) { foreach (var item in kvp.Value) { if (kvp.Key == typeof(Text)) Clean<Text>(item, path, prefab); else if (kvp.Key == typeof(RichText)) Clean<RichText>(item, path, prefab); else if (kvp.Key == typeof(TMP_Text)) Clean<TMP_Text>(item, path, prefab); } } PrefabUtility.UnloadPrefabContents(prefab); } AssetDatabase.SaveAssets(); EditorUtility.ClearProgressBar(); } cnt += "\n\n清理完成"; }
private static void Clean<T>(Component item, string path, GameObject prefab) { var textProperty = typeof(T).GetProperty("text"); if (textProperty != null) { var text = (string)textProperty.GetValue(item); if (!string.IsNullOrEmpty(text)) { cnt += $"\n预制体:{path} 文本路径:{item.transform.parent.name}/{item.name} 文本:{text}"; textProperty.SetValue(item, ""); PrefabUtility.SaveAsPrefabAsset(prefab, path); } } } }
|