我是靠谱客的博主 淡定超短裙,这篇文章主要介绍Unity多语言转换工具的实现,现在分享给大家,希望可以做个参考。

本文实例为大家分享了Unity多语言转换工具的具体代码,供大家参考,具体内容如下

说明

遍历Unity场景和Prefab,提取Text组件文字,并导出Json表。可将Json文本进行多语言翻译后,利用工具将内容替换回原场景或Prefab。

代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class ChangeTextLanguageTool : MonoBehaviour { [ExecuteInEditMode] [MenuItem("LanguageTool/一键提取Prefab中文字")] private static void GetAllPrefab() { LoadPath(Application.dataPath); } static void LoadPath(string path) { TextData _temData = new TextData(); string genPath = path; string[] filesPath = Directory.GetFiles(genPath, "*.prefab", SearchOption.AllDirectories); for (int i = 0; i < filesPath.Length; i++) { PrefabData _tempPrefab = new PrefabData(); filesPath[i] = filesPath[i].Substring(filesPath[i].IndexOf("Assets")); GameObject _prefab = AssetDatabase.LoadAssetAtPath(filesPath[i], typeof(GameObject)) as GameObject; GameObject prefabGameobject = PrefabUtility.InstantiatePrefab(_prefab) as GameObject; _tempPrefab.PrefabName = prefabGameobject.name; Text[] _tempAllText = prefabGameobject.transform.GetComponentsInChildren<Text>(true); for (int j = 0; j < _tempAllText.Length; j++) { if (!string.IsNullOrEmpty(_tempAllText[j].text)) { TextItemData _tempItemdata = new TextItemData(); _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 (int i = 0; i < ScenePath.Length; i++) { PrefabData _tempPrefab = new PrefabData(); ScenePath[i] = ScenePath[i].Substring(ScenePath[i].IndexOf("Assets")); Debug.Log(ScenePath[i]); if (ScenePath[i].Contains("Live2D")) continue; _tempPrefab.PrefabName = ScenePath[i]; EditorSceneManager.OpenScene(ScenePath[i], OpenSceneMode.Single); var _temp = Resources.FindObjectsOfTypeAll(typeof(Text)) as Text[]; //_temp.Select(data => data.gameObject.scene.isLoaded); foreach (Text obj in _temp) { if(!obj.gameObject.scene.isLoaded) continue; //Debug.LogError(obj.text); if (!string.IsNullOrEmpty(obj.text)) { TextItemData _tempItemdata = new TextItemData(); _tempItemdata._ObjName = obj.gameObject.name; _tempItemdata._TextContext = obj.text; _tempPrefab._PrefabData.Add(_tempItemdata); } } if (_tempPrefab._PrefabData.Count > 0) _temData.Data.Add(_tempPrefab); } Save(_temData); } static void Save(TextData data) { string Path = Application.dataPath + "/LanguageTool.json"; /*if (!File.Exists(Path)) File.Create(Path);*/ string json = JsonUtility.ToJson(data); File.WriteAllText(Path, json); EditorUtility.DisplayDialog("成功", "Prefab文本提取处理成功", "确定"); } static Dictionary<string, PrefabData> m_dicTextData = new Dictionary<string, PrefabData>(); [ExecuteInEditMode] [MenuItem("LanguageTool/一键替换LanguageTool字体")] static void ChangText() { TextAsset text = Resources.Load<TextAsset>("LanguageTool"); TextData _data = JsonUtility.FromJson<TextData>(text.text); m_dicTextData = new Dictionary<string, PrefabData>(); foreach (var item in _data.Data) { m_dicTextData[item.PrefabName] = item; } /*ChangeLabelText(Application.dataPath + "/App"); ChangeLabelText(Application.dataPath + "/Cosmos"); ChangeLabelText(Application.dataPath + "/Scenes");*/ ChangeLabelText(Application.dataPath); EditorUtility.DisplayDialog("成功", "替换成功", "确定"); } static void ChangeLabelText(string path) { string genPath = path; int m = 0; string[] filesPath = Directory.GetFiles(genPath, "*.prefab", SearchOption.AllDirectories); for (int i = 0; i < filesPath.Length; i++) { filesPath[i] = filesPath[i].Substring(filesPath[i].IndexOf("Assets")); GameObject _prefab = AssetDatabase.LoadAssetAtPath(filesPath[i], typeof(GameObject)) as GameObject; GameObject prefabGameobject = PrefabUtility.InstantiatePrefab(_prefab) as GameObject; PrefabData _tempData = null; if (m_dicTextData.ContainsKey(prefabGameobject.name)) { _tempData = m_dicTextData[prefabGameobject.name]; } Text[] _tempAllText = prefabGameobject.transform.GetComponentsInChildren<Text>(true); for (int j = 0; j < _tempAllText.Length; j++) { if (!string.IsNullOrEmpty(_tempAllText[j].text)) { if (null != _tempData && _tempData._PrefabData.Count > 0) { for (int z = 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 (int i = 0; i < ScenePath.Length; i++) { ScenePath[i] = ScenePath[i].Substring(ScenePath[i].IndexOf("Assets")); Debug.Log(ScenePath[i]); if (ScenePath[i].Contains("Live2D")) continue; PrefabData _tempData = null; if (m_dicTextData.ContainsKey(ScenePath[i])) { _tempData = m_dicTextData[ScenePath[i]]; } Scene _tempScene = EditorSceneManager.OpenScene(ScenePath[i], OpenSceneMode.Single); //foreach (Text obj in UnityEngine.Object.FindObjectsOfType(typeof(Text))) var _temp = Resources.FindObjectsOfTypeAll(typeof(Text)) as Text[]; //_temp.Select(data => data.gameObject.scene.isLoaded); foreach (Text obj in _temp) { if(!obj.gameObject.scene.isLoaded) continue; if (!string.IsNullOrEmpty(obj.text)) { if (null != _tempData && _tempData._PrefabData.Count > 0) { for (int z = 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] public class TextData { public List<PrefabData> Data = new List<PrefabData>(); } [Serializable] public class PrefabData { public string PrefabName = string.Empty; public List<TextItemData> _PrefabData = new List<TextItemData>(); } [Serializable] public class TextItemData { public string _ObjName = string.Empty; public string _TextContext = string.Empty; }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。

最后

以上就是淡定超短裙最近收集整理的关于Unity多语言转换工具的实现的全部内容,更多相关Unity多语言转换工具内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(144)

评论列表共有 0 条评论

立即
投稿
返回
顶部