需求如上一篇。
预设中很多UILabel,是在编辑器阶段直接赋值,因此需要逐个替换,可以将简体和繁体替换的文字做成字典,简体为key,繁体为value。
对UIlabel中的text进行逐行替换。
需要注意换行符
上代码
[MenuItem("Assets/misc/批量替换text中的中文")] static void 批量替换text中的中文() { Dictionary<string, string> replaceDic = new Dictionary<string, string>();//替换列表,示例 replaceDic.Add("简体第一行", "繁体第一行"); replaceDic.Add("简体第二行", "繁体第二行"); EditorUtility.DisplayProgressBar("处理中 请稍等", "请勿操作", 0); StringBuilder info = new StringBuilder(); var gameObjects = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets); for (int i = 0; i < gameObjects.Length; i++) { EditorUtility.DisplayProgressBar("处理中 请稍等", "正在处理:" + gameObjects[i].name, (i + 1f) / gameObjects.Length); string path = AssetDatabase.GetAssetPath(gameObjects[i]); if (!path.Contains(".prefab")) { continue; } GameObject go = PrefabUtility.InstantiatePrefab(gameObjects[i]) as GameObject; UILabel[] uiLabel = go.GetComponentsInChildren<UILabel>(true); //获取物体路径 if (uiLabel.Length > 0) { for (int j = 0; j < uiLabel.Length; j++) { string[] str = uiLabel[j].text.Split(" ".ToCharArray()); for (int k = 0; k < str.Length; k++) { if (replaceDic.ContainsKey(str[k])) { string key = str[k]; str[k] = replaceDic[key]; } } string allStr = ""; for (int k = 0; k < str.Length; k++) { allStr += str[k]; if (k != str.Length - 1) { allStr += " "; } } uiLabel[j].text = allStr; } } PrefabUtility.ReplacePrefab(go, gameObjects[i]);//替换 Object.DestroyImmediate(go); } AssetDatabase.SaveAssets();//保存 AssetDatabase.Refresh(); EditorUtility.ClearProgressBar(); }