Unity多语言转换工具的实现
本文实例为大家分享了Unity多语言转换工具的具体代码,供大家参考,具体内容如下
说明
遍历Unity场景和Prefab,提取Text组件文字,并导出Json表。可将Json文本进行多语言翻译后,利用工具将内容替换回原场景或Prefab。
代码
usingSystem; usingSystem.Collections; usingSystem.Collections.Generic; usingSystem.IO; usingSystem.Linq; usingUnityEditor; usingUnityEditor.SceneManagement; usingUnityEngine; usingUnityEngine.SceneManagement; usingUnityEngine.UI; publicclassChangeTextLanguageTool:MonoBehaviour { [ExecuteInEditMode] [MenuItem("LanguageTool/一键提取Prefab中文字")] privatestaticvoidGetAllPrefab() { LoadPath(Application.dataPath); } staticvoidLoadPath(stringpath) { TextData_temData=newTextData(); stringgenPath=path; string[]filesPath=Directory.GetFiles(genPath,"*.prefab",SearchOption.AllDirectories); for(inti=0;i(true); for(intj=0;j<_tempAllText.Length;j++) { if(!string.IsNullOrEmpty(_tempAllText[j].text)) { TextItemData_tempItemdata=newTextItemData(); _tempItemdata._ObjName=_tempAllText[j].gameObject.name; _tempItemdata._TextContext=_tempAllText[j].text; _tempPrefab._PrefabData.Add(_tempItemdata); } } if(_tempPrefab._PrefabData.Count>0) _temData.Data.Add(_tempPrefab); Debug.Log("遍历完成==========="+prefabGameobject.name); MonoBehaviour.DestroyImmediate(prefabGameobject); } string[]ScenePath=Directory.GetFiles(genPath,"*.unity",SearchOption.AllDirectories); for(inti=0;i data.gameObject.scene.isLoaded); foreach(Textobjin_temp) { if(!obj.gameObject.scene.isLoaded) continue; //Debug.LogError(obj.text); if(!string.IsNullOrEmpty(obj.text)) { TextItemData_tempItemdata=newTextItemData(); _tempItemdata._ObjName=obj.gameObject.name; _tempItemdata._TextContext=obj.text; _tempPrefab._PrefabData.Add(_tempItemdata); } } if(_tempPrefab._PrefabData.Count>0) _temData.Data.Add(_tempPrefab); } Save(_temData); } staticvoidSave(TextDatadata) { stringPath=Application.dataPath+"/LanguageTool.json"; /*if(!File.Exists(Path)) File.Create(Path);*/ stringjson=JsonUtility.ToJson(data); File.WriteAllText(Path,json); EditorUtility.DisplayDialog("成功","Prefab文本提取处理成功","确定"); } staticDictionary m_dicTextData=newDictionary (); [ExecuteInEditMode] [MenuItem("LanguageTool/一键替换LanguageTool字体")] staticvoidChangText() { TextAssettext=Resources.Load ("LanguageTool"); TextData_data=JsonUtility.FromJson (text.text); m_dicTextData=newDictionary (); foreach(varitemin_data.Data) { m_dicTextData[item.PrefabName]=item; } /*ChangeLabelText(Application.dataPath+"/App"); ChangeLabelText(Application.dataPath+"/Cosmos"); ChangeLabelText(Application.dataPath+"/Scenes");*/ ChangeLabelText(Application.dataPath); EditorUtility.DisplayDialog("成功","替换成功","确定"); } staticvoidChangeLabelText(stringpath) { stringgenPath=path; intm=0; string[]filesPath=Directory.GetFiles(genPath,"*.prefab",SearchOption.AllDirectories); for(inti=0;i (true); for(intj=0;j<_tempAllText.Length;j++) { if(!string.IsNullOrEmpty(_tempAllText[j].text)) { if(null!=_tempData&&_tempData._PrefabData.Count>0) { for(intz=0;z<_tempData._PrefabData.Count;z++) if(_tempData._PrefabData[z]._ObjName==_tempAllText[j].gameObject.name) { _tempAllText[j].text=_tempData._PrefabData[z]._TextContext; _tempData._PrefabData.RemoveAt(z); break; } } } } PrefabUtility.SaveAsPrefabAsset(prefabGameobject,filesPath[i]); Debug.Log("遍历完成==========="+prefabGameobject.name); MonoBehaviour.DestroyImmediate(prefabGameobject); AssetDatabase.SaveAssets(); } string[]ScenePath=Directory.GetFiles(genPath,"*.unity",SearchOption.AllDirectories); for(inti=0;i data.gameObject.scene.isLoaded); foreach(Textobjin_temp) { if(!obj.gameObject.scene.isLoaded) continue; if(!string.IsNullOrEmpty(obj.text)) { if(null!=_tempData&&_tempData._PrefabData.Count>0) { for(intz=0;z<_tempData._PrefabData.Count;z++) if(_tempData._PrefabData[z]._ObjName==obj.gameObject.name) { obj.text=_tempData._PrefabData[z]._TextContext; _tempData._PrefabData.RemoveAt(z); break; } } } } EditorSceneManager.SaveScene(_tempScene); AssetDatabase.SaveAssets(); } } } [Serializable] publicclassTextData { publicList Data=newList (); } [Serializable] publicclassPrefabData { publicstringPrefabName=string.Empty; publicList _PrefabData=newList (); } [Serializable] publicclassTextItemData { publicstring_ObjName=string.Empty; publicstring_TextContext=string.Empty; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。