-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUILabelTools.cs
298 lines (251 loc) · 9.96 KB
/
UILabelTools.cs
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using LitJson;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using UnityEditor;
using UnityEngine;
using Color = System.Drawing.Color;
public class UILabelTools : Editor
{
#region field
private const string extend = "txt";
private static readonly string[] PrefabPath =
{
"Assets/Resources/Prefabs/UI"
};
private static TxtClass[] txtClasses;
#endregion
#region MenuItem Editor
[MenuItem("Tools/海外版本工具/查找UILabel中文导出txt")]
private static void FindUILabel()
{
var filePath = GetSavePath("UILabel中文分析报告", "txt");
ReadJson();
if (File.Exists(filePath)) File.Delete(filePath);
var streamWriter = File.CreateText(filePath);
var prefabs = AssetDatabase.FindAssets("t:Prefab", PrefabPath);
for (var i = 0; i < prefabs.Length; i++)
{
var path = AssetDatabase.GUIDToAssetPath(prefabs[i]);
var go = AssetDatabase.LoadAssetAtPath<GameObject>(path);
var labels = go.transform.GetComponentsInChildren<UILabel>(true);
streamWriter.WriteLine("//----------------------------" + go.name + "-----------------------------");
for (var j = 0; j < labels.Length; j++)
if (HasChinese(labels[j].text))
streamWriter.WriteLine("{0}\t{1}\t{2}\n", path, GetGameObjectPath(labels[j].gameObject),
labels[j].text);
streamWriter.WriteLine("//----------------------------" + "Lua代码" + "-------------------------");
foreach (var label in labels)
if (HasChinese(label.text))
streamWriter.WriteLine(GetLuaComponent(label.gameObject, label.text, go));
streamWriter.WriteLine("");
EditorUtility.DisplayProgressBar("查找中", path, (float) i / (prefabs.Length - 1));
}
EditorUtility.ClearProgressBar();
streamWriter.Flush();
streamWriter.Close();
txtClasses = null;
Debug.Log("生成成功");
}
private static string GetSavePath(string file, string extend)
{
string saveDir = Environment.CurrentDirectory + "/海外版本";
if (!Directory.Exists(saveDir))
Directory.CreateDirectory(saveDir);
return string.Format("{0}/{1}_{2}.{3}", saveDir, DateTime.Now.ToString("yyyyMMddHHmm"), file,
extend);
}
private static string GetGameObjectPath(GameObject gameObject)
{
var path = "/" + gameObject.name;
while (gameObject.transform.parent != null)
{
gameObject = gameObject.transform.parent.gameObject;
path = "/" + gameObject.name + path;
}
return path;
}
private static string GetLuaComponent(GameObject gameObject, string txt, GameObject root)
{
var path = GetGameObjectPath(gameObject).Remove(0, 2 + root.name.Length);
var lua = string.Format(
"this.transform:Find(\"{0}\"):GetComponent(\"UILabel\").text = TextMgr.GetLanguageText({1})", path,
RegexCn(txt));
return lua;
}
/// <summary>
/// 判断一个字符中是否包含中文
/// </summary>
/// <param name="str">源字符串</param>
/// <returns>检测结果</returns>
public static bool HasChinese(string str)
{
return Regex.IsMatch(str, @"[\u4e00-\u9fa5]");
}
private static void ReadJson()
{
txtClasses = JsonMapper.ToObject<TxtClass[]>(Resources.Load<TextAsset>("TxtTableJson").text);
}
private static int RegexCn(string str)
{
for (var i = 0; i < txtClasses.Length; i++)
if (str.Equals(txtClasses[i].Cn))
return txtClasses[i].ID;
Debug.LogError("匹配出错在TxtTable里面匹配不到字符串(" + str + ")");
return 10010;
}
[MenuItem("Tools/海外版本工具/查找UILabel中文导出Excel")]
public static void WriteExcel()
{
var path = GetSavePath("UILabel中文分析报告", "xlsx");
var file = new FileInfo(path);
if (file.Exists)
{
file.Delete();
file = new FileInfo(path);
}
ReadJson();
//获取所有的Prefab的GUID
var excelConfigs = new Dictionary<string, ExcelConfig>();
var assets = AssetDatabase.FindAssets("t:Prefab", PrefabPath);
for (var i = 0; i < assets.Length; i++)
{
//通过GUID获取Asset路径
var guid = AssetDatabase.GUIDToAssetPath(assets[i]);
//加载Asset通过路径
var gameObject = AssetDatabase.LoadAssetAtPath<GameObject>(guid);
var labels = gameObject.GetComponentsInChildren<UILabel>();
foreach (var uiLabel in labels)
if (HasChinese(uiLabel.text))
{
if (excelConfigs.ContainsKey(gameObject.name))
{
excelConfigs[gameObject.name].uiLabelCompnents.Add(uiLabel);
}
else
{
var config = new ExcelConfig();
config.prefabPath = guid;
config.componentPath = GetGameObjectPath(uiLabel.gameObject).Remove(0, 1);
config.uiLabelCompnents = new List<UILabel>();
config.uiLabelCompnents.Add(uiLabel);
excelConfigs.Add(gameObject.name, config);
}
}
EditorUtility.DisplayProgressBar("查找中", guid, (float) i / (assets.Length - 1));
}
var row = 1;
using (var package = new ExcelPackage(file))
{
var worksheet = package.Workbook.Worksheets.Add("中文分析报告");
//设置表头
worksheet.Cells[row, 1].Value = "Prefab路径";
worksheet.Cells[row, 2].Value = "组件层级路径";
worksheet.Cells[row, 3].Value = "组件中文值";
//设置表头为粗体
using (var range = worksheet.Cells[row, 1, row, 3])
{
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
range.Style.Fill.BackgroundColor.SetColor(Color.AntiqueWhite);
range.Style.Font.Bold = true;
}
row++;
//row = 2
var count = 0;
foreach (var excelConfig in excelConfigs.Values)
{
worksheet.Cells[row, 1].Value =
AssetDatabase.LoadAssetAtPath<GameObject>(excelConfig.prefabPath).name;
worksheet.Cells[row, 1].Style.Font.Bold = true;
using (var range = worksheet.Cells[row, 1, row, 3])
{
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
range.Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml("#DDEBF7"));
}
row++;
for (var i = 0; i < excelConfig.uiLabelCompnents.Count; i++)
{
var label = excelConfig.uiLabelCompnents[i];
worksheet.Cells[row, 1].Value = excelConfig.prefabPath;
worksheet.Cells[row, 2].Value = GetGameObjectPath(label.gameObject).Remove(0, 1);
worksheet.Cells[row, 3].Value = label.text;
row++;
}
count++;
row++;
EditorUtility.DisplayProgressBar("写入Excel中", excelConfig.prefabPath,
(float) count / (excelConfigs.Count - 1));
}
package.Save();
}
excelConfigs = null;
EditorUtility.ClearProgressBar();
AssetDatabase.Refresh();
}
private static List<UILabel> GetUILabelsHasCN(UILabel[] uiLabels)
{
var labels = new List<UILabel>();
for (var i = 0; i < uiLabels.Length; i++)
if (HasChinese(uiLabels[i].text))
labels.Add(uiLabels[i]);
return labels;
}
#endregion
#region GameObject Editor
[MenuItem("GameObject/NGUI/检查子物体UILabel是否有中文", false, 10)]
public static void CheckChinese()
{
foreach (var gameObject in Selection.gameObjects)
{
var uiLables = gameObject.GetComponentsInChildren<UILabel>();
var count = 0;
foreach (var item in uiLables)
if (HasChinese(item.text))
{
Debug.Log(item.gameObject.name + "\t" + item.text);
count++;
}
Debug.Log("有" + count + "个UILabel带有中文");
}
}
[MenuItem("GameObject/NGUI/把UILabel赋值为空字符", false, 10)]
public static void ClearUILabel()
{
foreach (var gameObject in Selection.gameObjects)
{
var uiLables = gameObject.GetComponentsInChildren<UILabel>();
foreach (var item in uiLables)
if (HasChinese(item.text))
item.text = string.Empty;
;
}
}
#endregion
#region Bean
private class ConentConfig
{
public const string EXCEL_PREFAB_PATH_TITLE = "Prefab路径";
public const string COMPONENT_PATH_TITLE = "组件层级路径";
public const string COMPONENT_VALUE = "组件中文值";
public const string TXT_EXTENSION = "txt";
public const string EXCEL_EXTENSION = "xlsx";
public const string FILE_SAVE_NAME = "组件中文分析报告";
}
private class ExcelConfig
{
public string prefabPath { get; set; }
public string componentPath { get; set; }
public List<UILabel> uiLabelCompnents { get; set; }
}
private class TxtClass
{
public int ID { get; set; }
public string Cn { get; set; }
public string En { get; set; }
}
#endregion
}