using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System.IO; using UnityEngine.UI; using vnxbqy.UI; using System.Text; public class ImportPrefabsLanguage : EditorWindow { private static string topFilePath = Application.dataPath + "/ResourcesOut/UI"; [MenuItem("策划工具/==给打标记预制体进行翻译==", false)] private static void Export() { Debug.Log("工具说明:语言表必须已翻译好,多执行几次直到不再修改!!! 一般只在新地区需要翻译的时候使用一次即可,后续开发由程序正常提取到语言表,不再使用此工具"); string readPath = "Assets\\ResourcesOut\\Refdata\\Config\\Language.txt"; if (!File.Exists(readPath)) { Debug.LogError("语言表Assets\\ResourcesOut\\Refdata\\Config\\Language.txt 不存在"); return; } string[] lines = File.ReadAllLines(readPath, Encoding.UTF8); if (lines.Length == 0) { Debug.LogError("读语言表失败"); } else { Debug.Log("读语言表成功 " + lines.Length); } Dictionary dic = new Dictionary(); for (int i = 3; i < lines.Length; i++) { string[] info = lines[i].Split('\t'); string key; try { key = info[0]; if (dic.ContainsKey(key)) { continue; } dic[key] = info[1]; } catch (System.Exception) { Debug.LogError("解析失败" + lines[i] + " - 行号: " + i); continue; } } string[] filepaths = Directory.GetFiles(topFilePath, "*.prefab", SearchOption.AllDirectories); Debug.LogFormat("UI预制体文字路径{0}, {1}", topFilePath, filepaths.Length); 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; } string file = filepaths[startIndex]; file = file.Substring(file.IndexOf("Assets")); bool isCancel = EditorUtility.DisplayCancelableProgressBar("翻译预制体文字-" + filepaths.Length, file, (float)startIndex / (float)filepaths.Length); GameObject _prefab = AssetDatabase.LoadAssetAtPath(file); UITextMark[] transArr14 = _prefab.GetComponentsInChildren(true); foreach (UITextMark item in transArr14) { Text tmpText = item.GetComponent(); if (tmpText == null) { Debug.LogErrorFormat("异常:第{0}行翻译失败{1},{2}", startIndex, file, item.name); continue; } if (!dic.ContainsKey(item.language)) { Debug.LogErrorFormat("异常:第{0}行翻译失败{1},{2}, 语言表 key不存在 {3}", startIndex, file, item.name, item.language); continue; } if (tmpText.text != dic[item.language]) { //Debug.LogFormat("翻译成功--{0} , {1}; {2} - {3}", file, item.name, tmpText.text, dic[item.language]); tmpText.text = dic[item.language]; EditorUtility.SetDirty(_prefab); changeCnt++; } } startIndex++; if (isCancel || startIndex >= filepaths.Length) { Debug.LogFormat("翻译结束-- 检索{0}个预制体,共翻译{1}个文本", startIndex, changeCnt); EditorUtility.ClearProgressBar(); EditorApplication.update = null; startIndex = 0; AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } }; } } //[MenuItem("策划工具/==批量给预制体打标记==", false)] //private static void Export() //{ // string writePath = "importUITextMark.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 != 7) // { // Debug.LogFormat("异常:长度不对--执行到:第{0}行 {1}", startIndex, lineStr); // return; // } // string file = info[0]; // int index = int.Parse(info[1]); // string name = info[2]; // string content = info[5]; // 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 mark = item.GetComponent(); // if (mark == null) // { // mark = item.gameObject.AddComponent(); // mark.language = content; // EditorUtility.SetDirty(_prefab); // changeCnt++; // //Debug.LogFormat("字体批量文件--{0} , {1}", file, index); // } // else if (mark.language != content) // { // Debug.LogErrorFormat("异常:第{0}行替换失败{1},{2}", startIndex, file, name); // } // } // 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(); // } // }; // Debug.LogFormat("导入翻译完成"); //} //导出和语言表共用的key,如果没有生成唯一key //private static void Export() //{ // // 将港台的中文 写入到越南的表中 // //Language import3 // string readPath = "Assets\\ResourcesOut\\Refdata\\Config\\Language.txt"; // if (!File.Exists(readPath)) // { // return; // } // string[] lines = File.ReadAllLines(readPath, Encoding.UTF8); // if (lines.Length == 0) // { // Debug.LogError("读语言表失败"); // } // else // { // Debug.Log("读语言表成功 " + lines.Length); // } // Dictionary dic = new Dictionary(); // for (int i = 3; i < lines.Length; i++) // { // string[] info = lines[i].Split('\t'); // string key; // try // { // key = info[1]; // } // catch (System.Exception) // { // Debug.LogError("读语言表失败" + lines[i] + " - " + i); // continue; // } // if (dic.ContainsKey(key)) // { // continue; // } // dic[key] = info[0]; // } // string[] linesZH = File.ReadAllLines("import3.txt", Encoding.UTF8); // Dictionary dicZH = new Dictionary(); // SortedDictionary dicZHList = new SortedDictionary(); // for (int i = 0; i < linesZH.Length; i++) // { // string[] info = linesZH[i].Split('\t'); // var key = info[0] + "_" + info[1] + "_" + info[2]; // dicZHList[key] = info; // if (string.IsNullOrEmpty(info[5].Trim())) // { // continue; // } // if (dicZH.ContainsKey(info[6])) // { // continue; // } // dicZH[info[6]] = info[5]; // } // string writePath = "exportUILanguage3.txt"; // if (File.Exists(writePath)) // { // File.Delete(writePath); // } // FileStream fs = new FileStream(writePath, FileMode.Create);//如不存在,则创建新的文本文档 // //1.找语言表有的写入line // //2.找语言表没有的 查找缓存,缓存没有的写入缓存 并写入line // foreach (var item in dicZHList) // { // var key = item.Key; // var line = item.Value; // var lineList = new List(line); // bool islan = false; // if (string.IsNullOrEmpty(lineList[5].Trim())) // { // if (dic.ContainsKey(lineList[6])) // { // lineList[5] = dic[lineList[6]]; // islan = true; // } // else if (dicZH.ContainsKey(lineList[6])) // { // lineList[5] = dicZH[lineList[6]]; // } // else // { // dicZH[lineList[6]] = lineList[3]; // lineList[5] = lineList[3]; // } // } // else // { // if (dic.ContainsKey(lineList[6])) // { // islan = true; // } // } // string newLine = string.Join("\t", lineList); // newLine = newLine + "\t" + islan + "\r\n"; // byte[] data1 = System.Text.Encoding.UTF8.GetBytes(newLine); // fs.Write(data1, 0, data1.Length); // } // fs.Close(); // Debug.LogFormat("导出完成"); //} // 将key读取从text的内容 移植到 keyname中 //public class ImportPrefabsLanguage : EditorWindow //{ // [MenuItem("策划工具/==批量导入预制体语言表设置==", false)] // private static void Export() // { // string writePath = "importUILanguage.txt"; // if (!File.Exists(writePath)) // { // return; // } // string[] lines = File.ReadAllLines(writePath, Encoding.UTF8); // int startIndex = 1; // 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 != 8) // { // Debug.LogFormat("异常:长度不对--执行到:第{0}行 {1}", startIndex, lineStr); // return; // } // string file = info[0]; // int index = int.Parse(info[1]); // string name = info[2]; // string content = info[4].Replace("\r", "").Replace("\n", ""); // int state; // int.TryParse(info[7], out state); // bool isCancel = EditorUtility.DisplayCancelableProgressBar("导入预制体语言key-" + 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) // { // if (state == 1) // { // bool reset = false; // TextEx tmpTextEx = item as TextEx; //TextImage 是TextEx的子类 // if ((tmpTextEx != null && tmpTextEx.isKey)) // { // if (tmpTextEx.keyName != content) // { // tmpTextEx.keyName = content; // reset = true; // } // } // RichText tmpRichText = item as RichText; // if ((tmpRichText != null && tmpRichText.language)) // { // if (tmpRichText.enableDisplay != content) // { // tmpRichText.enableDisplay = content; // reset = true; // } // } // if (reset) // { // 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("文字key批量修改成功-- 检索{0}行,共替换{1}行", startIndex, changeCnt); // EditorUtility.ClearProgressBar(); // EditorApplication.update = null; // startIndex = 0; // AssetDatabase.SaveAssets(); // AssetDatabase.Refresh(); // } // }; // } //}