using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; using UnityEngine.UI; using vnxbqy.UI; using System.Text; //批量导入文字到预制体的text中 //!!!需要注意预制体导入的index要和导出的一致,避免遇到同名时无法区分的问题 public class ImportPrefabsWords : EditorWindow { [MenuItem("策划工具/==批量导入预制体文字==", false)] private static void Export() { string writePath = "import.txt"; if (!File.Exists(writePath)) { return; } string[] lines = File.ReadAllLines(writePath, Encoding.UTF8); int startIndex = 0; int changeCnt = 0; EditorApplication.update = delegate () { if (startIndex >= lines.Length) { Debug.LogFormat("文字批量修改成功-- 检索{0}行,共替换{1}行", startIndex, changeCnt); EditorUtility.ClearProgressBar(); EditorApplication.update = null; startIndex = 0; AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); return; } var lineStr = lines[startIndex]; string[] info = lines[startIndex].Split('\t'); startIndex++; if (info.Length != 5) { Debug.LogFormat("异常:长度不对--执行到:第{0}行 {1}", startIndex, lineStr); return; } string file = info[0]; int index = int.Parse(info[1]); string name = info[2]; string content = info[3].Replace("\r","").Replace("\n",""); bool isCancel = EditorUtility.DisplayCancelableProgressBar("导入预制体文字-"+lines.Length, file, (float)startIndex / (float)lines.Length); GameObject _prefab = AssetDatabase.LoadAssetAtPath(file); if (_prefab == null) { if (isCancel) { EditorUtility.ClearProgressBar(); EditorApplication.update = null; startIndex = 0; AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } Debug.LogFormat("异常:预制体异常--执行到:第{0}行 {1}", startIndex, lineStr); return; } Text[] transArr = _prefab.GetComponentsInChildren(true); Text item = transArr[index]; if (item.name == name) { var curText = item.text.Replace("\r", "").Replace("\n", ""); if (curText != content) { item.text = content; EditorUtility.SetDirty(_prefab); changeCnt++; //Debug.LogFormat("字体批量文件--{0} , {1}", file, index); } } else { Debug.LogErrorFormat("异常:第{0}行替换失败{1},{2}", startIndex, file, name); } if (isCancel || startIndex >= lines.Length) { Debug.LogFormat("文字批量修改成功-- 检索{0}行,共替换{1}行", startIndex, changeCnt); EditorUtility.ClearProgressBar(); EditorApplication.update = null; startIndex = 0; AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } }; } }