/* 字符串替换工具 */ using UnityEditor; using UnityEngine; using System.IO; public class QuickReplaceWordsTool : EditorWindow { enum FileType : int//文件类型 { All = 0, Lua = 1, Prefab = 2, Csv = 3, Txt = 4, } enum OpType : int//操作类型 { Common = 1, //普通操作 SwitchToSpecial = 2, //快速替换中文标点符号为特殊字符串 SwitchToBack = 3, //快速还原特殊字符串为中文标点符号 } private static string luaPath = @"H:\Tomorrow\develop\code\u3d\Lua"; private static string prefabPath = @"H:\Tomorrow\develop\code\u3d\Assets\LuaFramework\AssetBundleRes\ui"; private static string csvPath = @"H:\Tomorrow\develop\exchange"; private static string txtPath = @"H:\Tomorrow\develop\exchange"; static string replaceWord_last = "文本批量替换脚本QuickReplaceWordsTool"; static string replaceWord_new = "文本批量替换脚本QuickReplaceWordsTool"; [MenuItem("Tools/Quick Replace Words Tool")] public static void ShowWindow() { EditorWindow.GetWindow(typeof(QuickReplaceWordsTool)); } void OnGUI() { GUILayout.Label("字符串替换:", EditorStyles.boldLabel); GUILayout.Label("被替换的单词为:", EditorStyles.boldLabel); replaceWord_last = EditorGUILayout.TextArea(replaceWord_last); GUILayout.Label("替换后的单词为:", EditorStyles.boldLabel); replaceWord_new = EditorGUILayout.TextArea(replaceWord_new); GUILayout.Label("替换Lua脚本的路径为:", EditorStyles.boldLabel); luaPath = EditorGUILayout.TextArea(luaPath); if (GUILayout.Button("替换lua脚本文件")) ShowWarnTip(OpType.Common,FileType.Lua,"替换lua脚本文件"); GUILayout.Label("替换prefab的路径为:", EditorStyles.boldLabel); prefabPath = EditorGUILayout.TextArea(prefabPath); if (GUILayout.Button("替换prefab文件")) ShowWarnTip(OpType.Common,FileType.Prefab,"替换prefab文件"); GUILayout.Label("替换csv的路径为:", EditorStyles.boldLabel); csvPath = EditorGUILayout.TextArea(csvPath); if (GUILayout.Button("替换csv文件")) ShowWarnTip(OpType.Common,FileType.Csv,"替换csv文件"); GUILayout.Label("替换txt的路径为:", EditorStyles.boldLabel); txtPath = EditorGUILayout.TextArea(txtPath); if (GUILayout.Button("替换txt文件")) ShowWarnTip(OpType.Common,FileType.Txt,"替换txt文件"); GUILayout.Label("\n外文翻译的时候,需要翻译之前处理中文符号,\n翻译操作后还原,避免中文符号引起的部分匹配问题。\n详见ReplaceWordsToolTest脚本", EditorStyles.boldLabel); if (GUILayout.Button("快速替换中文标点符号为特殊字符串")) ShowWarnTip(OpType.SwitchToSpecial, FileType.All,"快速替换中文标点符号为特殊字符串"); if (GUILayout.Button("快速还原特殊字符串为中文标点符号")) ShowWarnTip(OpType.SwitchToBack, FileType.All,"快速还原特殊字符串为中文标点符号"); } static void ShowWarnTip(OpType opType, FileType fileType, string tipLabel){ if (EditorUtility.DisplayDialog("!!替换请确认!!", "!!请留意确定你的操作!!\n!!请留意确定你的操作!!\n!!请确认替换路径!!\n确认进行" + tipLabel, "确定")) { SetPathFileWord(opType, fileType); } } //要处理的目录 static void SetPathFileWord(OpType opType, FileType fileType) { string modify_path = luaPath; string prefab_path = prefabPath; string csv_path = csvPath; string txt_path = txtPath; string[] curPrefabs = { "", "", "" }; int startIndex = 0; string[] files = {}; string[] filesLua = {}; string[] filesCsv = {}; string[] filesPrefab = {}; string[] filesTxt = {}; switch (fileType) { case FileType.All: filesCsv = Directory.Exists(csv_path)?Directory.GetFiles(csv_path, "*.csv", SearchOption.AllDirectories):filesCsv; filesPrefab = Directory.Exists(prefab_path)?Directory.GetFiles(prefab_path, "*.prefab", SearchOption.AllDirectories):filesPrefab; filesLua = Directory.Exists(modify_path)?Directory.GetFiles(modify_path, "*.lua", SearchOption.AllDirectories):filesLua; filesTxt = Directory.Exists(txt_path)?Directory.GetFiles(txt_path, "*.txt", SearchOption.AllDirectories):filesTxt; break; case FileType.Csv: files = Directory.Exists(csv_path)?Directory.GetFiles(csv_path, "*.csv", SearchOption.AllDirectories):files; break; case FileType.Prefab: files = Directory.Exists(prefab_path)?Directory.GetFiles(prefab_path, "*.prefab", SearchOption.AllDirectories):files; break; case FileType.Lua: files = Directory.Exists(modify_path)?Directory.GetFiles(modify_path, "*.lua", SearchOption.AllDirectories):files; break; default: files = Directory.Exists(txt_path)?Directory.GetFiles(txt_path, "*.txt", SearchOption.AllDirectories):files; break; } EditorApplication.update = delegate () { string filePath = ""; double allLen = filesCsv.Length + filesPrefab.Length + filesLua.Length + files.Length + filesTxt.Length; /*------------------------------*/ if (fileType == FileType.All){ if (startIndex <= filesTxt.Length - 1){ filePath = filesTxt.Length>0?GetRelativeAssetsPath(FileType.Txt, filesTxt[startIndex]):"txt路径不存在(@#!%*&)"; } else if (startIndex <= filesTxt.Length + filesCsv.Length - 1){ filePath = filesCsv.Length>0?GetRelativeAssetsPath(FileType.Csv, filesCsv[startIndex - filesTxt.Length]):"csv路径不存在(@#!%*&)"; } else if (startIndex <= filesTxt.Length + filesCsv.Length + filesLua.Length - 1){ filePath = filesLua.Length>0?GetRelativeAssetsPath(FileType.Lua, filesLua[startIndex - filesCsv.Length - filesTxt.Length]):"lua路径不存在(@#!%*&)"; } else{ filePath = filesPrefab.Length>0?GetRelativeAssetsPath(FileType.Prefab, filesPrefab[startIndex - filesCsv.Length - filesLua.Length - filesTxt.Length]):"prefab路径不存在(@#!%*&)"; } } else{ filePath = files.Length>0?GetRelativeAssetsPath(fileType, files[startIndex]):"检索路径不存在(@#!%*&)"; } if (!filePath.Contains("路径不存在(@#!%*&)")) SetArabicSupToLua(opType, filePath); /*------------------------------*/ Debug.Log("filePath:" + filePath); bool isCancel = EditorUtility.DisplayCancelableProgressBar(string.Format("资源处理中({0}/{1})", startIndex, allLen), filePath, (float)startIndex / (float)allLen); startIndex++; if (isCancel || startIndex >= allLen) { AssetDatabase.Refresh(); AssetDatabase.SaveAssets(); EditorUtility.ClearProgressBar(); EditorApplication.update = null; startIndex = 0; Debug.Log("处理完成"); } }; } static private string GetRelativeAssetsPath(FileType fileType, string path) { if (fileType == FileType.Prefab){ // 预制体路径比较特别!!! return "Assets" + Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace('\\', '/'); } else { return Path.GetFullPath(path).Replace(Path.GetFullPath(Application.dataPath), "").Replace('\\', '/'); } } static string SetArabicWord(string data) { string a = replaceWord_last; string b = replaceWord_new; data = data.Replace(a, b); return data; } static string SetSwitchToSpecialWord(string data) { data = data.Replace(",", "(6.4.8.4)"); data = data.Replace("。", "(1.2.3.4)"); data = data.Replace("!", "(4.3.2.1)"); data = data.Replace("(", "(9.4.5.4)"); data = data.Replace(")", "(6.4.2.4)"); return data; } static string SetSwitchToBackWord(string data) { data = data.Replace("(6.4.8.4)", ","); data = data.Replace("(1.2.3.4)", "。"); data = data.Replace("(4.3.2.1)", "!"); data = data.Replace("(9.4.5.4)", "("); data = data.Replace("(6.4.2.4)", ")"); return data; } static void SetArabicSupToLua(OpType opType ,string path) { string saveStr; using (StreamReader reader = new StreamReader(path)) { string data = reader.ReadToEnd(); switch (opType) { case OpType.SwitchToBack: saveStr = SetSwitchToBackWord(data); break; case OpType.SwitchToSpecial: saveStr = SetSwitchToSpecialWord(data); break; default: saveStr = SetArabicWord(data); break; } } using (StreamWriter sw = new StreamWriter(path)) { sw.Write(saveStr); } } }