forked from XINCGer/Unity3DTraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreakAtlasTool.cs
482 lines (378 loc) · 15.6 KB
/
BreakAtlasTool.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System;
using System.IO;
public static class BreakAtlasTool {
private static UIAtlas ua;
private static Texture source;
private static List<UISpriteData> spriteList = null;
private static string rootPath = "";
//--------------拆分图集--------------------
[MenuItem("AtlasTool/BreakAtlas")]
static void StartBreakAtlas () {
rootPath = Application.dataPath + "/BreakAtlas/";
DirectoryInfo dir = new DirectoryInfo("Assets/TGameResources/SubSys/AllAtlas");
List<string> list = GetDirectoryFile(dir);
DirectoryInfo[] dirs = dir.GetDirectories();
for (int i = 0; i < dirs.Length; i++)
{
list.AddRange(GetDirectoryFile(dirs[i]));
DirectoryInfo[] mDirs = null;
mDirs = dirs[i].GetDirectories();
if (mDirs.Length != 0) {
for (int j = 0; j < mDirs.Length; j++)
{
list.AddRange(GetDirectoryFile(mDirs[j]));
}
}
}
string targetPath = "";
for (int j = 0; j < list.Count; j++)
{
EditorUtility.DisplayProgressBar(string.Format("拆分{0}", list[j]), j.ToString(), j / list.Count);
UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(list[j], typeof(UIAtlas));
if (obj == null)
Debug.Log(list[j] + " is null");
ua = obj as UIAtlas;
spriteList = ua.spriteList;
Rect r = new Rect();
source = ua.texture;
Texture2D tt2 = NGUIEditorTools.ImportTexture(ua.texture, true, true, false);
targetPath = list[j].Replace("Assets/TGameResources/","");
string[] strs = targetPath.Split('/');
targetPath = "";
for (int i = 0; i < strs.Length-1; i++)
{
targetPath += "/";
targetPath += strs[i];
}
for (int i = 0; i < spriteList.Count; i++)
{
r.x = spriteList[i].x;
r.y = source.height - spriteList[i].y - spriteList[i].height;
r.width = spriteList[i].width;
r.height = spriteList[i].height;
GetBytes(source, r, ua.name, spriteList[i].name, spriteList[i].paddingLeft, spriteList[i].paddingRight, spriteList[i].paddingTop, spriteList[i].paddingBottom, targetPath);
}
}
AssetDatabase.Refresh();
EditorUtility.ClearProgressBar();
Debug.LogError("拆分完毕");
}
static void GetBytes(Texture _sourceTex, Rect sourceRect, string atlasName, string spriteName, int paddingLeft, int paddingRight, int paddingTop, int paddingBottom,string targetPath)
{
Texture2D sourceTex = _sourceTex as Texture2D;
int x = Mathf.FloorToInt(sourceRect.x);
int y = Mathf.FloorToInt(sourceRect.y);
int width = Mathf.FloorToInt(sourceRect.width);
int height = Mathf.FloorToInt(sourceRect.height);
Color[] pix = sourceTex.GetPixels(x, y, width, height);
int mWidth = width + paddingLeft + paddingRight;
int mHeight = height + paddingTop + paddingBottom;
int count = mWidth * mHeight;
Texture2D destTex = new Texture2D(mWidth, mHeight);
for (int i = 0; i < mHeight; i++)
{
for (int j = 0; j < mWidth; j++)
{
if ((i < paddingBottom || i > paddingBottom + height) || (j<paddingLeft || j>paddingLeft+width))
{
//Debug.LogError("-----------------");
destTex.SetPixel(j,i, new Color(0, 0, 0, 0));
}
else
{
//destTex.SetPixel(j, i, new Color(0, 0.3f, 0, 1));
Color c = sourceTex.GetPixel(x + j - paddingLeft , y + i - paddingBottom);
destTex.SetPixel(j, i, c);
}
}
}
destTex.Apply();
byte [] bytes ;
bytes = destTex.EncodeToPNG();
GetPic(bytes,atlasName,spriteName,targetPath);
}
static void GetPic(byte[] bytes, string atlasName, string spriteName,string TargetPath)
{
String targetPath = Application.dataPath.Replace("Assets", "") + "BreakAtlasPic" + TargetPath + "/";
if (!Directory.Exists(targetPath + atlasName))
Directory.CreateDirectory(targetPath + atlasName);
File.WriteAllBytes(targetPath + atlasName + "/" + spriteName + ".png", bytes);
//Debug.LogError("targetPath:" + targetPath+","+atlasName);
}
static List<string> GetPath(DirectoryInfo dir)
{
List<string> list = new List<string>();
//获取文件下的子文件夹
DirectoryInfo[] dirs = dir.GetDirectories();
for (int i = 0; i < dirs.Length; i++)
{
list.AddRange(GetDirectoryFile(dirs[i]));
}
list.AddRange(GetDirectoryFile(dir));
return list;
}
static List<string> GetDirectoryFile(DirectoryInfo dir)
{
List<string> list = new List<string>();
//当前文件夹下的所有Prefab
FileInfo[] infos = dir.GetFiles();
foreach (var item in infos)
{
if (item.FullName.Contains(".prefab") && !item.FullName.Contains(".prefab.meta"))
{
string str = "";
str = item.FullName;
str = str.Replace(@"\", @"/");
str = str.Replace(Application.dataPath, "Assets");
list.Add(str);
// Debug.LogError(item.FullName + ",new:" + str);
}
}
return list;
}
//--------------拆分图集结束--------------------
//--------------合新图集-------------------
[MenuItem("AtlasTool/NewAtlas")]
static void GetNewAtlas() {
List<string> list = new List<string>();
rootPath = "Assets/TGameResources/SubSys/AllAtlas";
//String targetPath = Application.dataPath.Replace("Assets", "") + "BreakAtlasPic" + TargetPath + "/";
string picPath = "";
picPath = "Assets/" + "BreakAtlasPic/SubSys" + "/AllAtlas";
DirectoryInfo dir = new DirectoryInfo(picPath);
DirectoryInfo[] dirs = dir.GetDirectories();
for (int i = 0; i < dirs.Length; i++)
{
GetNewAtlas(dirs[i]);
//Debug.Log("---------" + dirs[i].FullName + "----------");
}
AssetDatabase.Refresh();
Debug.Log("---------------合图集完毕-----------------");
}
/// <summary>
/// 传进文件夹,将本文件夹下的所有文件打成以本文件夹为名的Atlas
/// </summary>
/// <param name="dir"></param>
private static void GetNewAtlas(DirectoryInfo dir)
{
FileInfo[] infos = dir.GetFiles();
bool haveInfo = false;
List<string> list = new List<string>();
foreach (var item in infos)
{
if (item.FullName.Contains(".png") && !item.FullName.Contains(".png.meta"))
{
haveInfo = true;
string str = "";
str = item.FullName;
str = str.Replace(@"\", @"/");
str = str.Replace(Application.dataPath, "Assets");
list.Add(str);
}
}
if (!haveInfo)
{
DirectoryInfo[] infos1 = dir.GetDirectories();
for (int i = 0; i < infos1.Length; i++)
{
GetNewAtlas(infos1[i]);
}
return;
}
List<Texture> texTures = new List<Texture>();
for (int i = 0; i < list.Count; i++)
{
UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(list[i], typeof(Texture));
FileImporterSetting.SetFileImporterSetting(EFileImporterSettingType.TextureForRGBA32, 1024, AssetDatabase.GetAssetPath(obj));
texTures.Add(obj as Texture);
//Debug.LogError("Texture:" +i+":"+ list[i]);
}
string dirPath = dir.FullName.Replace(@"\", @"/");
dirPath = dirPath.Replace(Application.dataPath + "/BreakAtlasPic/", "Assets/TGameResources/");
string[] dirPaths = dirPath.Split('/');
dirPath = "Assets";
for (int i = 1; i < dirPaths.Length - 1; i++)
{
dirPath += "/";
dirPath += dirPaths[i];
}
//Debug.LogError("dirPath:" + dirPath);
//return;
//Debug.LogError("dirPath:" + dirPath);
UnityEngine.Object obj1 = AssetDatabase.LoadAssetAtPath(dirPath + "/" + dirPaths[dirPaths.Length-1] + ".prefab", typeof(UIAtlas));
ua = obj1 as UIAtlas;
UIAtlasUtil.UncompressAtlas(dirPath + "/" + dirPaths[dirPaths.Length-1] + ".prefab");
FileImporterSetting.SetFileImporterSetting(EFileImporterSettingType.TextureForRGBA32, 1024, AssetDatabase.GetAssetPath(ua.spriteMaterial.mainTexture));
//(ua.spriteMaterial.mainTexture);
spriteList = ua.spriteList;
List<UIAtlasMaker.SpriteEntry> ui = UIAtlasMaker.CreateSprites(texTures);
for (int i = 0; i < ui.Count; i++)
{
for (int j = 0; j < spriteList.Count; j++)
{
if (ui[i].name == spriteList[j].name)
{
ui[i].borderLeft = spriteList[j].borderLeft;
ui[i].borderRight = spriteList[j].borderRight;
ui[i].borderBottom = spriteList[j].borderBottom;
ui[i].borderTop = spriteList[j].borderTop;
}
}
}
//ua.UpdateAtlas();
UIAtlasMaker.UpdateAtlas(ua, ui);
UIAtlasUtil.CompressAtlas(dirPath + "/" + dirPaths[dirPaths.Length-1] + ".prefab");
//UnityEngine.Object obj1 = AssetDatabase.LoadAssetAtPath("Assets/zcyAtlas/LobbySys_Atlas1.prefab", typeof(UIAtlas));
//ua = obj1 as UIAtlas;
AssetDatabase.Refresh();
Debug.Log("---------图集打包完毕-----------");
}
//fullName转成Assets 路径
static string GetFileAdr(string fullPath) {
string str = "";
str = fullPath;
str = str.Replace(@"\", @"/");
str = str.Replace(Application.dataPath, "Assets");
return str;
}
//------------合新图集结束-------------------
[MenuItem("AtlasTool/更新图集")]
public static void UpdateAtlas()
{
List<string> list = new List<string>();
rootPath = "Assets/TGameResources/SubSys/AllAtlas";
string picPath = "";
picPath = "Assets/" + "BreakAtlasPic/SubSys" + "/AllAtlas";
DirectoryInfo dir = new DirectoryInfo(picPath);
DirectoryInfo[] dirs = dir.GetDirectories();
for (int i = 0; i < dirs.Length; i++)
{
UpdateAtlas(dirs[i]);
}
AssetDatabase.Refresh();
}
private static void UpdateAtlas(DirectoryInfo dir)
{
FileInfo[] infos = dir.GetFiles();
bool haveInfo = false;
List<string> list = new List<string>();
foreach (var item in infos)
{
if (item.FullName.Contains(".png") && !item.FullName.Contains(".png.meta"))
{
haveInfo = true;
string str = "";
str = item.FullName;
str = str.Replace(@"\", @"/");
str = str.Replace(Application.dataPath, "Assets");
list.Add(str);
}
}
if (!haveInfo)
{
DirectoryInfo[] infos1 = dir.GetDirectories();
for (int i = 0; i < infos1.Length; i++)
{
GetNewAtlas(infos1[i]);
}
return;
}
List<Texture> texTures = new List<Texture>();
for (int i = 0; i < list.Count; i++)
{
UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(list[i], typeof(Texture));
FileImporterSetting.SetFileImporterSetting(EFileImporterSettingType.TextureForRGBA32, 1024, AssetDatabase.GetAssetPath(obj));
texTures.Add(obj as Texture);
}
string dirPath = dir.FullName.Replace(@"\", @"/");
dirPath = dirPath.Replace(Application.dataPath + "/BreakAtlasPic/", "Assets/TGameResources/");
string[] dirPaths = dirPath.Split('/');
dirPath = "Assets";
for (int i = 1; i < dirPaths.Length - 1; i++)
{
dirPath += "/";
dirPath += dirPaths[i];
}
UnityEngine.Object obj1 = AssetDatabase.LoadAssetAtPath(dirPath + "/" + dirPaths[dirPaths.Length - 1] + ".prefab", typeof(UIAtlas));
ua = obj1 as UIAtlas;
UIAtlasUtil.UncompressAtlas(dirPath + "/" + dirPaths[dirPaths.Length - 1] + ".prefab");
FileImporterSetting.SetFileImporterSetting(EFileImporterSettingType.TextureForRGBA32, 1024, AssetDatabase.GetAssetPath(ua.spriteMaterial.mainTexture));
spriteList = ua.spriteList;
List<UIAtlasMaker.SpriteEntry> ui = UIAtlasMaker.CreateSprites(texTures);
for (int i = 0; i < ui.Count; i++)
{
for (int j = 0; j < spriteList.Count; j++)
{
if (ui[i].name == spriteList[j].name)
{
ui[i].borderLeft = spriteList[j].borderLeft;
ui[i].borderRight = spriteList[j].borderRight;
ui[i].borderBottom = spriteList[j].borderBottom;
ui[i].borderTop = spriteList[j].borderTop;
}
}
}
for (int i = 0; i < ui.Count; i++)
{
UIAtlasMaker.AddOrUpdate(ua,ui[i]);
}
UIAtlasUtil.CompressAtlas(dirPath + "/" + dirPaths[dirPaths.Length - 1] + ".prefab");
AssetDatabase.Refresh();
}
/// <summary>
/// 不拆分图集只生成目录
/// </summary>
[MenuItem("AtlasTool/生成目录")]
public static void GenerateDirs()
{
rootPath = Application.dataPath + "/BreakAtlas/";
DirectoryInfo dir = new DirectoryInfo("Assets/TGameResources/SubSys/AllAtlas");
List<string> list = GetDirectoryFile(dir);
DirectoryInfo[] dirs = dir.GetDirectories();
for (int i = 0; i < dirs.Length; i++)
{
list.AddRange(GetDirectoryFile(dirs[i]));
DirectoryInfo[] mDirs = null;
mDirs = dirs[i].GetDirectories();
if (mDirs.Length != 0)
{
for (int j = 0; j < mDirs.Length; j++)
{
list.AddRange(GetDirectoryFile(mDirs[j]));
}
}
}
string targetPath = "";
for (int j = 0; j < list.Count; j++)
{
UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(list[j], typeof(UIAtlas));
if (obj == null)
Debug.Log(list[j] + " is null");
ua = obj as UIAtlas;
targetPath = list[j].Replace("Assets/TGameResources/", "");
string[] strs = targetPath.Split('/');
targetPath = "";
for (int i = 0; i < strs.Length - 1; i++)
{
targetPath += "/";
targetPath += strs[i];
}
MKDirs(ua.name, targetPath);
}
}
/// <summary>
/// 创建空的目录
/// </summary>
/// <param name="atlasName"></param>
/// <param name="TargetPath"></param>
private static void MKDirs(string atlasName, string TargetPath)
{
String targetPath = Application.dataPath.Replace("Assets", "") + "BreakAtlasPic" + TargetPath + "/";
if (!Directory.Exists(targetPath + atlasName))
Directory.CreateDirectory(targetPath + atlasName);
}
}