Created
July 25, 2018 03:22
-
-
Save yangruihan/3174c0ebef119fe43504d5a47c4fb987 to your computer and use it in GitHub Desktop.
Prefab 文本内容查找及替换(用于查找和替换指定目录下 Prefab 内 Text 组件包含的文本)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections; | |
| using System.Collections.Generic; | |
| using System.IO; | |
| using UnityEditor; | |
| using UnityEngine; | |
| using UnityEngine.UI; | |
| /// <summary> | |
| /// Prefab 中文本扫描替换 | |
| /// </summary> | |
| public class PrefabTextScanner : EditorWindow | |
| { | |
| public static string UI_PREFAB_ROOT_PATH | |
| { | |
| get { return Application.dataPath + "/Resources/Prefabs/UI"; } | |
| } | |
| private class DataInfo | |
| { | |
| public GameObject Obj; | |
| public List<Text> Texts; | |
| } | |
| private static string keyText = ""; | |
| private static string newKeyText = ""; | |
| private static string oldSearchText = ""; | |
| private static bool fuzzySearch = false; | |
| private static List<DataInfo> resultList = new List<DataInfo>(); | |
| private static List<Text> resultTextList = new List<Text>(); | |
| private Vector2 _scrollViewPos; | |
| [MenuItem("Tools/搜索包含指定内容的Prefab")] | |
| public static void ShowWindow() | |
| { | |
| var window = GetWindow<PrefabTextScanner>(); | |
| window.Show(); | |
| resultList.Clear(); | |
| resultTextList.Clear(); | |
| } | |
| public void OnGUI() | |
| { | |
| GUILayout.BeginVertical(); | |
| GUILayout.Label("关键字"); | |
| keyText = EditorGUILayout.TextArea(keyText, GUILayout.Width(220)); | |
| GUILayout.Label("新值"); | |
| newKeyText = EditorGUILayout.TextArea(newKeyText, GUILayout.Width(220)); | |
| fuzzySearch = EditorGUILayout.Toggle("是否开启模糊搜索", fuzzySearch); | |
| if (GUILayout.Button("查询")) | |
| { | |
| if (string.IsNullOrEmpty(keyText)) | |
| { | |
| EditorUtility.DisplayDialog("查询失败", "关键字不能为空", "确定"); | |
| } | |
| else | |
| { | |
| Search(); | |
| } | |
| } | |
| if (GUILayout.Button("替换")) | |
| { | |
| if (string.IsNullOrEmpty(keyText)) | |
| { | |
| if (EditorUtility.DisplayDialog("提示", "新值为空", "是否确定替换")) | |
| { | |
| Replace(); | |
| } | |
| } | |
| else | |
| { | |
| Replace(); | |
| } | |
| } | |
| _scrollViewPos = EditorGUILayout.BeginScrollView(_scrollViewPos, GUILayout.Width(400), GUILayout.Height(200)); | |
| for (int i = 0; i < resultList.Count; i++) | |
| { | |
| EditorGUILayout.BeginHorizontal(); | |
| var data = resultList[i]; | |
| if (GUILayout.Button(data.Obj.name)) | |
| { | |
| EditorGUIUtility.PingObject(data.Obj); | |
| Selection.activeGameObject = data.Obj; | |
| } | |
| GUILayout.Space(20); | |
| foreach (var text in data.Texts) | |
| { | |
| if (GUILayout.Button(text.name)) | |
| { | |
| EditorGUIUtility.PingObject(text); | |
| Selection.activeGameObject = text.gameObject; | |
| } | |
| } | |
| EditorGUILayout.EndHorizontal(); | |
| } | |
| EditorGUILayout.EndScrollView(); | |
| GUILayout.EndVertical(); | |
| } | |
| private static void Search() | |
| { | |
| resultList.Clear(); | |
| resultTextList.Clear(); | |
| var prefabs = LoadDiectoryPrefab(UI_PREFAB_ROOT_PATH); | |
| if (prefabs != null) | |
| { | |
| oldSearchText = keyText; | |
| EditorUtility.DisplayProgressBar("搜索中...", "开始搜索", 0f); | |
| for (int i = 0, count = prefabs.Count; i < count; i++) | |
| { | |
| var prefab = prefabs[i]; | |
| EditorUtility.DisplayProgressBar("搜索中...", prefab.name, (i + 1f) / count); | |
| Text[] texts = prefab.GetComponentsInChildren<Text>(true); | |
| DataInfo dataInfo = null; | |
| foreach (var text in texts) | |
| { | |
| if (fuzzySearch && text.text.Contains(keyText)) | |
| { | |
| if (dataInfo == null) | |
| { | |
| dataInfo = new DataInfo(); | |
| dataInfo.Obj = prefab; | |
| dataInfo.Texts = new List<Text>(); | |
| resultList.Add(dataInfo); | |
| } | |
| dataInfo.Texts.Add(text); | |
| resultTextList.Add(text); | |
| break; | |
| } | |
| else if (!fuzzySearch && text.text == keyText) | |
| { | |
| if (dataInfo == null) | |
| { | |
| dataInfo = new DataInfo(); | |
| dataInfo.Obj = prefab; | |
| dataInfo.Texts = new List<Text>(); | |
| resultList.Add(dataInfo); | |
| } | |
| dataInfo.Texts.Add(text); | |
| resultTextList.Add(text); | |
| break; | |
| } | |
| } | |
| } | |
| EditorUtility.ClearProgressBar(); | |
| } | |
| else | |
| { | |
| EditorUtility.DisplayDialog("查询失败", "Prefab路径错误", "确定"); | |
| } | |
| } | |
| private static void Replace() | |
| { | |
| if (oldSearchText != keyText) | |
| { | |
| Search(); | |
| } | |
| if (resultTextList == null || resultTextList.Count == 0) | |
| { | |
| EditorUtility.DisplayDialog("替换失败", "没有找到匹配的Prefab,请先点击搜索", "确定"); | |
| return; | |
| } | |
| EditorUtility.DisplayProgressBar("替换中...", "开始搜索", 0f); | |
| for (int i = 0, count = resultTextList.Count; i < count; i++) | |
| { | |
| var text = resultTextList[i]; | |
| EditorUtility.DisplayProgressBar("替换中...", text.transform.root.name, (i + 1f) / count); | |
| text.text = newKeyText; | |
| } | |
| EditorUtility.ClearProgressBar(); | |
| AssetDatabase.SaveAssets(); | |
| AssetDatabase.Refresh(); | |
| EditorUtility.DisplayDialog("替换完成", "共替换 " + resultTextList.Count + " 处", "确定"); | |
| resultTextList.Clear(); | |
| } | |
| private static List<GameObject> LoadDiectoryPrefab(string rootPath) | |
| { | |
| var dictoryInfo = new DirectoryInfo(rootPath); | |
| if (!dictoryInfo.Exists) return null; | |
| var ret = new List<GameObject>(); | |
| FileInfo[] fileInfos = dictoryInfo.GetFiles("*.prefab", SearchOption.AllDirectories); | |
| EditorUtility.DisplayProgressBar("加载Prefab中...", "开始加载", 0f); | |
| int i = 0; | |
| foreach (FileInfo files in fileInfos) | |
| { | |
| string path = files.FullName; | |
| #if UNITY_STANDALONE_WIN | |
| string assetPath = path.Substring(path.IndexOf("Assets\\")); | |
| #else | |
| string assetPath = path.Substring(path.IndexOf("Assets/")); | |
| #endif | |
| EditorUtility.DisplayProgressBar("加载Prefab中...", assetPath, ++i * 1.0f / fileInfos.Length); | |
| GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath); | |
| ret.Add(prefab); | |
| } | |
| EditorUtility.ClearProgressBar(); | |
| return ret; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment