using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; using UnityEngine.UI; using Snxxz.UI; using System.Text; //批量导入文字到预制体的text中 //!!!需要注意预制体导入的index要和导出的一致,避免遇到同名时无法区分的问题 public class ImportPrefabsWords : EditorWindow { private static string topFilePath=Application.dataPath + "/ResourcesOut/UI"; [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; EditorApplication.update = delegate () { string[] info = lines[startIndex].Split('\t'); startIndex++; if (info.Length != 4) { if (startIndex >= lines.Length) { EditorUtility.ClearProgressBar(); EditorApplication.update = null; startIndex = 0; AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } return; } string file = info[0]; int index = int.Parse(info[1]); string name = info[2]; string content = info[3].Replace("\r\n","").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 || startIndex >= lines.Length) { EditorUtility.ClearProgressBar(); EditorApplication.update = null; startIndex = 0; AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } return; } Text[] transArr = _prefab.GetComponentsInChildren(true); Text item = transArr[index]; if (item.name == name) { item.text = content; EditorUtility.SetDirty(_prefab); }else { Debug.LogErrorFormat("第{0}行替换失败{1},{2}", startIndex+1, file, name); } if (isCancel || startIndex >= lines.Length) { EditorUtility.ClearProgressBar(); EditorApplication.update = null; startIndex = 0; AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } }; } }