Created
November 7, 2017 16:02
-
-
Save yangruihan/f18d18fbdc6a74297fdde986414683b9 to your computer and use it in GitHub Desktop.
uGUI 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; | |
| public class TextContentMatchUtil : EditorWindow | |
| { | |
| [MenuItem("Tools/Text组件内容匹配")] | |
| public static void ShowWindow() | |
| { | |
| var window = GetWindow<TextContentMatchUtil>(); | |
| window.Show(); | |
| } | |
| private string _matchContent; | |
| private Object _matchFolder; | |
| private void OnGUI() | |
| { | |
| GUILayout.BeginHorizontal(); | |
| GUILayout.Label("匹配内容"); | |
| _matchContent = EditorGUILayout.TextField(_matchContent); | |
| GUILayout.EndHorizontal(); | |
| GUILayout.Space(20); | |
| _matchFolder = EditorGUILayout.ObjectField("查找路径", _matchFolder, typeof(Object)); | |
| GUILayout.FlexibleSpace(); | |
| if (GUILayout.Button("开始匹配")) | |
| { | |
| StartMatch(); | |
| } | |
| } | |
| private void StartMatch() | |
| { | |
| var foldPath = Application.dataPath + AssetDatabase.GetAssetPath(_matchFolder).Replace("Assets", ""); | |
| var prefabs = LoadDiectoryPrefab(foldPath); | |
| // 显示进度条 | |
| EditorUtility.DisplayProgressBar("匹配Prefab中...", "开始匹配", 0f); | |
| int i = 0; | |
| foreach (var prefab in prefabs) | |
| { | |
| // 更新进度条 | |
| EditorUtility.DisplayProgressBar("匹配Prefab中...", prefab.name, ++i * 1.0f / prefabs.Count); | |
| var textComponents = prefab.GetComponentsInChildren<Text>(); | |
| foreach (var textComponent in textComponents) | |
| { | |
| if (textComponent.text.Contains(_matchContent)) | |
| { | |
| Handle(textComponent); | |
| } | |
| } | |
| } | |
| // 匹配完毕,关闭进度条 | |
| EditorUtility.ClearProgressBar(); | |
| } | |
| private void Handle(Text text) | |
| { | |
| Debug.Log(string.Format("--- 处理 {0}[{1}] ---", text.gameObject.transform.root.name, text.gameObject.name)); | |
| text.fontSize = 20; | |
| } | |
| private static List<GameObject> LoadDiectoryPrefab(string rootPath) | |
| { | |
| var dictoryInfo = new DirectoryInfo(rootPath); | |
| if (!dictoryInfo.Exists) return null; | |
| var ret = new List<GameObject>(); | |
| // 得到该目录下的所有 Prefab 文件 | |
| FileInfo[] fileInfos = dictoryInfo.GetFiles("*.prefab", SearchOption.AllDirectories); | |
| // 显示进度条 | |
| EditorUtility.DisplayProgressBar("加载Prefab中...", "开始加载", 0f); | |
| int i = 0; | |
| // 遍历所有文件 | |
| foreach (FileInfo files in fileInfos) | |
| { | |
| string path = files.FullName; | |
| // 将文件绝对路径改成 Unity 资源相对路径,形如 Assets/xxx/xxx | |
| #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); | |
| // 通过路径加载 Prefab | |
| GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath); | |
| // 将 Prefab 加到结果列表中 | |
| 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