| using System.Collections; | 
| using System.Collections.Generic; | 
| using UnityEngine; | 
| using UnityEditor; | 
| using System.IO; | 
| using UnityEngine.UI; | 
| using vnxbqy.UI; | 
| using System.Text; | 
| using System.Linq;  | 
| using System;  | 
|   | 
| //批量导入文本格式  | 
| public class ImportPrefabsWordFormat : EditorWindow  | 
| { | 
|   | 
|   | 
|     [MenuItem("策划工具/==批量导入预制体文本格式==", false)] | 
|     private static void Export() | 
|     { | 
|   | 
|         string writePath = "importFontSize.txt"; | 
|         if (!File.Exists(writePath)) | 
|         { | 
|             return; | 
|         } | 
|   | 
|         string[] lines = File.ReadAllLines(writePath, Encoding.UTF8); | 
|         //按文件分组 | 
|         Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>(); | 
|         for (int i = 0; i < lines.Length; i++) | 
|         {  | 
|             string[] info = lines[i].Split('\t');  | 
|             string file = info[0];  | 
|             if (!dic.ContainsKey(file))  | 
|             {  | 
|                 dic.Add(file, new List<string>());  | 
|             }  | 
|             dic[file].Add(lines[i]);  | 
|         } | 
|   | 
|         var fileList = dic.Keys.ToList(); | 
|   | 
|         int startIndex = 0; | 
|         int changeCnt = 0; | 
|         EditorApplication.update = delegate () { | 
|             if (startIndex >= fileList.Count) | 
|             { | 
|                 Debug.LogFormat("文本格式批量修改成功-- 检索{0}行,共替换{1}行", startIndex, changeCnt); | 
|                 EditorUtility.ClearProgressBar(); | 
|                 EditorApplication.update = null; | 
|                 startIndex = 0; | 
|                 AssetDatabase.SaveAssets(); | 
|                 AssetDatabase.Refresh(); | 
|                 return; | 
|             } | 
|             var file = fileList[startIndex]; | 
|             var singleFilelines = dic[fileList[startIndex]]; | 
|             startIndex++; | 
|   | 
|             bool isCancel = EditorUtility.DisplayCancelableProgressBar("导入预制体文本格式-" + fileList.Count, file, (float)startIndex /fileList.Count); | 
|             GameObject _prefab = AssetDatabase.LoadAssetAtPath<GameObject>(file); | 
|             if (_prefab == null) | 
|             { | 
|                 Debug.LogFormat("没有此文件 {0} - {1}", startIndex, file); | 
|                 return; | 
|             } | 
|             foreach (var line in singleFilelines)  | 
|             {  | 
|                 string[] info = line.Split('\t'); | 
|   | 
|                 int index = int.Parse(info[1]); | 
|                 string name = info[2]; | 
|   | 
|                 Text[] transArr = _prefab.GetComponentsInChildren<Text>(true); | 
|   | 
|                 if (index >= transArr.Length)  | 
|                 { | 
|                     Debug.LogFormat("第{0}个替换失败{1} 此文件文本越界", startIndex, file); | 
|                     break; | 
|                 } | 
|                 Text item = transArr[index]; | 
|                 if (item.name == name) | 
|                 {  | 
|   | 
|                     RectTransform rectTransform = item.GetComponent<RectTransform>();  | 
|   | 
|                     rectTransform.sizeDelta = new Vector2(float.Parse(info[3]), float.Parse(info[4]));  | 
|                     rectTransform.anchorMin = new Vector2(float.Parse(info[5]), float.Parse(info[6]));  | 
|                     rectTransform.anchorMax = new Vector2(float.Parse(info[7]), float.Parse(info[8]));  | 
|                     rectTransform.anchoredPosition = new Vector2(float.Parse(info[9]), float.Parse(info[10]));  | 
|                     rectTransform.pivot = new Vector2(float.Parse(info[11]), float.Parse(info[12]));  | 
|                     rectTransform.localScale = new Vector3(float.Parse(info[13]), float.Parse(info[14]), rectTransform.localScale.z);  | 
|                     item.fontSize = int.Parse(info[15]);  | 
|                     item.alignment = (TextAnchor)Enum.Parse(typeof(TextAnchor), info[16]);  | 
|                     item.lineSpacing = float.Parse(info[17]);  | 
|                     item.horizontalOverflow = (HorizontalWrapMode)Enum.Parse(typeof(HorizontalWrapMode), info[18]);  | 
|                     item.verticalOverflow = (VerticalWrapMode)Enum.Parse(typeof(VerticalWrapMode), info[19]);  | 
|                     item.resizeTextForBestFit = bool.Parse(info[20]);  | 
|                     item.resizeTextMinSize = int.Parse(info[21]);  | 
|                     item.resizeTextMaxSize = int.Parse(info[22]);  | 
|   | 
|                     EditorUtility.SetDirty(_prefab);  | 
|   | 
|                     changeCnt++; | 
|                 } | 
|                 else | 
|                 { | 
|                     Debug.LogErrorFormat("异常:第{0}行替换失败{1},{2}", startIndex, file, name); | 
|                 } | 
|              | 
|             } | 
|                  | 
|   | 
|             if (isCancel || startIndex >= lines.Length)  | 
|             { | 
|                 Debug.LogFormat("文字批量修改成功-- 检索{0}行,共替换{1}行", lines.Length, changeCnt); | 
|                 EditorUtility.ClearProgressBar(); | 
|                 EditorApplication.update = null; | 
|                 startIndex = 0; | 
|                 AssetDatabase.SaveAssets(); | 
|                 AssetDatabase.Refresh(); | 
|             } | 
|         };  | 
|   | 
|   | 
|     }  | 
|   | 
|   | 
| } |