|
|
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEditor;
- using UnityEngine;
- using UnityEngine.UI;
-
- /// <summary>
- /// 替换预设中Text文字工具
- /// </summary>
- public class PrefabTextReplaceTools : EditorWindow
- {
- private static PrefabTextReplaceTools instance;
- static string _targetTxt; //被替换的文字内容
- static string _replaceTxt = ""; //要替换的文字内容
- static string _pathRoot; //查找文件路径
- static int _rate = 100;
- static int _ratePrefab = 10;
- static int _successCount = 0;
-
- private static void Init()
- {
- instance = EditorWindow.GetWindow<PrefabTextReplaceTools>("替换预设文字内容");
- _pathRoot = Application.dataPath + "/LuaFramework/AssetBundleRes/ui";
- instance.position = new Rect(new Vector2(604, 290), new Vector2(650, 400));
- }
-
-
- [MenuItem("Tools/替换预设Text组件text属性内容")]
- static void ShowExcelTools()
- {
- Init();
- instance.Show();
- }
-
- void OnGUI()
- {
- DrawOptions();
- }
-
- private void DrawOptions()
- {
- _targetTxt = EditorGUILayout.TextField("被替换文字内容", _targetTxt);
- _replaceTxt = EditorGUILayout.TextField("要替换文字内容", _replaceTxt);
- EditorGUILayout.Space();
- _pathRoot = EditorGUILayout.TextField("查找目录", _pathRoot);
- EditorGUILayout.Space();
- if (GUILayout.Button("转换"))
- {
- EditorCoroutineRunner.StartEditorCoroutine(Conver());
- }
- EditorGUILayout.Space();
- EditorGUILayout.Space();
- EditorGUILayout.Space();
- _rate = EditorGUILayout.IntField("每帧处理 文件 数", _rate);
- _ratePrefab = EditorGUILayout.IntField("每帧处理 预设 数", _ratePrefab);
- EditorGUILayout.HelpBox("每帧处理文件数,如果处理过程太卡,可以减少,当然也可以增加。\n目的是为了不让进程出现(未响应)", MessageType.Info);
- }
-
- IEnumerator Conver()
- {
- _successCount = 0;
- var files = Directory.GetFiles(_pathRoot, "*prefab*", SearchOption.AllDirectories);
- List<string> prefabsPath = new List<string>();
- //找到所有预设
- float count = 0;
- foreach (var file in files)
- {
- count++;
- if (file.EndsWith(".prefab"))
- {
- var clearPaht = file.Replace(Application.dataPath, "Assets");
- prefabsPath.Add(clearPaht);
-
- if (count % _rate == 0)
- {
- yield return 1;
- }
- EditorUtility.DisplayProgressBar("查找文件中的预设···", "查找中 " + file, count / files.Length);
- }
- }
- count = 0;
- foreach (var singlePaht in prefabsPath)
- {
-
- count++;
- var prefab = AssetDatabase.LoadAssetAtPath<GameObject>(singlePaht);
-
- if (prefab)
- {
- //boo = false;
- CheckSingPrefab(prefab);
- //if (boo)
- //{
- // Debug.Log(prefab.name);
- //}
- }
- if (count % _ratePrefab == 0)
- {
- yield return 1;
- }
- EditorUtility.DisplayProgressBar("替换当前预设中Text内容", "当前 " + singlePaht, count / prefabsPath.Count);
- }
- yield return 1;
-
- AssetDatabase.SaveAssets();
- AssetDatabase.Refresh();
- EditorUtility.ClearProgressBar();
- EditorUtility.DisplayDialog("提示", "查找完成,一共替换 " + _successCount + " 处", "好的呢", "");
- }
- //private bool boo = false;
- /// <summary>
- /// 循环遍历对象的每一个子对象,执行检查操作
- /// </summary>
- /// <param name="go"></param>
- void CheckSingPrefab(GameObject go)
- {
-
- CheckSingGameObject(go);
- foreach (Transform child in go.transform)
- {
- if (child.childCount > 0)
- {
- CheckSingPrefab(child.gameObject);
- }
- CheckSingGameObject(child.gameObject);
- }
- }
-
- /// <summary>
- /// 检查当前对象上是否有Text组件,如果有检查文字内容是否包含指定的内容,如果有就替换成要被替换的文字内容
- /// </summary>
- /// <param name="go"></param>
- void CheckSingGameObject(GameObject go)
- {
- var Txt = go.GetComponent<Text>();
- if (Txt)
- {
- string curStr = Txt.text;
- curStr = curStr.ToLower();
- if (curStr.Contains(_targetTxt))
- {
- string new_Str = curStr.Replace(_targetTxt, _replaceTxt);
- _successCount++;
- //boo = true;
- Txt.text = new_Str;
- EditorUtility.SetDirty(Txt); //必须要先一下这个 才能保存到磁盘 当资源已改变并需要保存到磁盘,Unity内部使用dirty标识来查找
- }
-
- }
- }
- }
|