UGUIPackgToolWindow.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Reflection;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. using UnityEditor;
  11. using UnityEditor.U2D;
  12. using UnityEngine;
  13. using UnityEngine.U2D;
  14. using UnityEngine.UI.PackgTool;
  15. using Debug = UnityEngine.Debug;
  16. using Object = System.Object;
  17. namespace UnityEditor.UI
  18. {
  19. public class UGUIPackgToolWindow : EditorWindow
  20. {
  21. public class SVNInfo
  22. {
  23. public int yb;
  24. public int bb;
  25. public string url;
  26. }
  27. private Vector2 pos = Vector2.zero;
  28. private static string dbPath;
  29. public string findName;
  30. private static string packgPath;
  31. int bbh1 = 0;
  32. int bbh2 = 0;
  33. [UnityEditor.MenuItem("UI/图集")]
  34. public static void Open()
  35. {
  36. packgPath = Application.dataPath.Replace("Assets", "UGUIPack/");
  37. if (!System.IO.Directory.Exists(packgPath))
  38. {
  39. Directory.CreateDirectory(packgPath);
  40. }
  41. dbPath = packgPath + "uguiconfig.db";
  42. UGUIPackgToolWindow uguiPackgToolWindow = UGUIPackgToolWindow.GetWindow<UGUIPackgToolWindow>();
  43. uguiPackgToolWindow.Show();
  44. }
  45. public void SetSVNInfo()
  46. {
  47. SVNInfo svnInfo = BanBenXinXing($"info {UGUICacheInfo.uguiPackDB.uiPath2}");
  48. if (!string.IsNullOrEmpty(svnInfo.url))
  49. {
  50. SVNInfo svnInfo2 = BanBenXinXing($"info {svnInfo.url}");
  51. bbh1 = svnInfo2.bb;
  52. bbh2 = svnInfo.bb;
  53. }
  54. }
  55. public SVNInfo BanBenXinXing(string par)
  56. {
  57. if (!string.IsNullOrEmpty(UGUICacheInfo.uguiPackDB.uiPath2))
  58. {
  59. SVNInfo svnInfo = new SVNInfo();
  60. List<string> info = Proce(par);
  61. if (info.Count <= 0)
  62. {
  63. return svnInfo;
  64. }
  65. for (int i = 0; i < info.Count; i++)
  66. {
  67. int index = info[i].IndexOf(":");
  68. if (index <= 0)
  69. {
  70. continue;
  71. }
  72. string key = info[i].Substring(0, index);
  73. string value = info[i].Substring(index + 1);
  74. if (key.Equals("Revision"))
  75. {
  76. string v1 = value.Replace(" ", "");
  77. svnInfo.yb = int.Parse(v1);
  78. }
  79. if (key.Equals("Last Changed Rev"))
  80. {
  81. string v1 = value.Replace(" ", "");
  82. svnInfo.bb = int.Parse(v1);
  83. }
  84. if (key.Equals("URL"))
  85. {
  86. svnInfo.url = value;
  87. }
  88. }
  89. return svnInfo;
  90. }
  91. return null;
  92. }
  93. private void OnEnable()
  94. {
  95. SetSVNInfo();
  96. }
  97. private List<string> Proce(string par)
  98. {
  99. string allPath = UGUICacheInfo.uguiPackDB.uiPath2.Replace("unity_use_ui/", "工具/bin/svn.exe");
  100. ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo(allPath,
  101. par);
  102. processStartInfo.CreateNoWindow = true;
  103. processStartInfo.UseShellExecute = false;
  104. processStartInfo.RedirectStandardOutput = true;
  105. processStartInfo.RedirectStandardError = true;
  106. System.Diagnostics.Process process = new Process();
  107. process.StartInfo = processStartInfo;
  108. process.Start();
  109. process.WaitForExit();
  110. while (!process.StandardError.EndOfStream)
  111. {
  112. return new List<string>();
  113. }
  114. List<string> allInfo = new List<string>();
  115. while (!process.StandardOutput.EndOfStream)
  116. {
  117. allInfo.Add(process.StandardOutput.ReadLine());
  118. }
  119. process.Close();
  120. return allInfo;
  121. }
  122. private void OnGUI()
  123. {
  124. if (dbPath == null)
  125. {
  126. Open();
  127. }
  128. if (UGUICacheInfo.uguiPackDB == null)
  129. {
  130. return;
  131. }
  132. GUILayout.BeginHorizontal();
  133. Color color = GUI.color;
  134. if (bbh1 != 0 && bbh1 != bbh2)
  135. {
  136. GUI.color = Color.red;
  137. if (GUILayout.Button("SVN有新的内容,请更新SVN"))
  138. {
  139. }
  140. }
  141. else
  142. {
  143. }
  144. GUI.color = color;
  145. GUILayout.Label($"远程版本号{bbh1}___本地版本号{bbh2}");
  146. GUILayout.EndHorizontal();
  147. if (GUILayout.Button("清空缓存"))
  148. {
  149. PlayerPrefs.SetString("uiPath", "");
  150. UGUICacheInfo.Clare();
  151. }
  152. if (string.IsNullOrEmpty(UGUICacheInfo.uguiPackDB.uiPath2))
  153. {
  154. if (GUILayout.Button("选在服务器UI"))
  155. {
  156. string path = EditorUtility.OpenFolderPanel("选择来源文件夹", "", "") + "/";
  157. if (!File.Exists(path + "art.config"))
  158. {
  159. EditorUtility.DisplayDialog("错误提示", "选择的文件夹错误,没有找到对应的美术配置", "确定");
  160. }
  161. else
  162. {
  163. PlayerPrefs.SetString("uiPath", path);
  164. UGUICacheInfo.uguiPackDB.uiPath2 = path;
  165. SetSVNInfo();
  166. }
  167. }
  168. return;
  169. }
  170. else if (!UGUICacheInfo.uguiPackDB.isJiaoYan)
  171. {
  172. if (!File.Exists(UGUICacheInfo.uguiPackDB.uiPath2 + "art.config"))
  173. {
  174. EditorUtility.DisplayDialog("错误提示", "选择的文件夹错误,没有找到对应的美术配置", "确定");
  175. UGUICacheInfo.uguiPackDB.uiPath2 = null;
  176. return;
  177. }
  178. else
  179. {
  180. UGUICacheInfo.uguiPackDB.isJiaoYan = true;
  181. }
  182. }
  183. {
  184. if (GUILayout.Button("更新服务器UI到unity"))
  185. {
  186. UpdateArt();
  187. }
  188. }
  189. if (!UGUICacheInfo.uguiPackDB.isShowPack && GUILayout.Button("显示图集"))
  190. {
  191. UGUICacheInfo.AllTextrueJsons.Clear();
  192. string targetFilePath = Application.dataPath + "/Art/UIAssets/TextrueJson";
  193. if (!Directory.Exists(targetFilePath))
  194. {
  195. return;
  196. }
  197. UGUICacheInfo.uguiPackDB.ClearUselessPack();
  198. string[] allFile = System.IO.Directory.GetFiles(targetFilePath, "*.txt"); //当前所有的图集
  199. for (int i = 0; i < allFile.Length; i++)
  200. {
  201. TextrueJson textrueJson = JsonUtility.FromJson<TextrueJson>(File.ReadAllText(allFile[i]));
  202. UGUICacheInfo.AllTextrueJsons.Add(textrueJson);
  203. PackInfo packInfo = UGUICacheInfo.uguiPackDB.GetPackgInfo(textrueJson.packName);
  204. string currGuid = GetFileMD5(allFile[i]);
  205. if (packInfo == null)
  206. {
  207. packInfo = UGUICacheInfo.AddNewPackInfo(allFile[i].Replace(Application.dataPath, ""),
  208. textrueJson);
  209. }
  210. else if (!string.IsNullOrEmpty(packInfo.packgJsonPath))
  211. {
  212. if (!File.Exists(Application.dataPath + packInfo.packgJsonPath))
  213. {
  214. packInfo.packgJsonPath = allFile[i].Replace(Application.dataPath, "");
  215. }
  216. }
  217. if (string.IsNullOrEmpty(packInfo.lasetJsonGUID) || !currGuid.Equals(packInfo.lasetJsonGUID))
  218. {
  219. packInfo.isUpdate = true;
  220. }
  221. }
  222. UGUICacheInfo.uguiPackDB.isShowPack = true;
  223. string json = JsonUtility.ToJson(UGUICacheInfo.uguiPackDB);
  224. File.WriteAllText(dbPath, json);
  225. }
  226. if (UGUICacheInfo.uguiPackDB.isShowPack && GUILayout.Button("隐藏图集"))
  227. {
  228. UGUICacheInfo.uguiPackDB.isShowPack = false;
  229. }
  230. if (UGUICacheInfo.uguiPackDB.isShowPack)
  231. {
  232. ShowPack();
  233. }
  234. }
  235. private void ShowPack()
  236. {
  237. GUILayout.BeginHorizontal();
  238. EditorGUILayout.LabelField("筛查");
  239. findName = UnityEditor.EditorGUILayout.TextArea(findName);
  240. if (GUILayout.Button("一键"))
  241. {
  242. try
  243. {
  244. for (int j = 0; j < UGUICacheInfo.uguiPackDB.allPackgInfos.Count; j++)
  245. {
  246. PackInfo packInfo = UGUICacheInfo.uguiPackDB.allPackgInfos[j];
  247. SpriteAtlas spriteAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(packInfo.packgSpritePath);
  248. if (spriteAtlas == null)
  249. {
  250. continue;
  251. }
  252. string json = File.ReadAllText(Application.dataPath + packInfo.packgJsonPath);
  253. TextrueJson textrueJson = JsonUtility.FromJson<TextrueJson>(json);
  254. List<UnityEngine.Object> allAddAsset = new List<UnityEngine.Object>();
  255. UnityEngine.Object[] currObject = spriteAtlas.GetPackables();
  256. spriteAtlas.Remove(currObject);
  257. for (int i = 0; i < textrueJson.newTextureJson.Count; i++)
  258. {
  259. if (textrueJson.newTextureJson[i].textrueName.Contains("bg_"))
  260. {
  261. continue;
  262. }
  263. if (string.IsNullOrEmpty(textrueJson.newTextureJson[i].textrueName))
  264. {
  265. continue;
  266. }
  267. TextrueJson tongYong = UGUICacheInfo.GetTongYong(textrueJson.newTextureJson[i].textrueName);
  268. if (tongYong != null && !tongYong.packName.Equals(packInfo.packName))
  269. {
  270. continue;
  271. }
  272. // string[] guidUI = AssetDatabase.FindAssets(textrueName);
  273. string textrueName = textrueJson.newTextureJson[i].textrueName;
  274. string[] guidUI = AssetDatabase.FindAssets(textrueName);
  275. if (guidUI != null && guidUI.Length > 0)
  276. {
  277. for (int k = 0; k < guidUI.Length; k++)
  278. {
  279. string path = AssetDatabase.GUIDToAssetPath(guidUI[k]);
  280. if (!path.Contains(".png"))
  281. {
  282. continue;
  283. }
  284. UnityEngine.Object obj = AssetDatabase.LoadMainAssetAtPath(path);
  285. TextureImporter emojiTex = AssetImporter.GetAtPath(path) as TextureImporter;
  286. if (emojiTex != null && emojiTex.textureType != TextureImporterType.Sprite)
  287. {
  288. emojiTex.textureType = TextureImporterType.Sprite;
  289. emojiTex.SaveAndReimport();
  290. }
  291. if (!obj.name.Equals(textrueName))
  292. {
  293. continue;
  294. }
  295. allAddAsset.Add(obj);
  296. break;
  297. }
  298. // string path = AssetDatabase.GUIDToAssetPath(guidUI[0]);
  299. // if (!path.Contains(".png"))
  300. // {
  301. // continue;
  302. // }
  303. //
  304. // UnityEngine.Object obj = AssetDatabase.LoadMainAssetAtPath(path);
  305. // TextureImporter emojiTex = AssetImporter.GetAtPath(path) as TextureImporter;
  306. // if (emojiTex != null && emojiTex.textureType != TextureImporterType.Sprite)
  307. // {
  308. // emojiTex.textureType = TextureImporterType.Sprite;
  309. // emojiTex.SaveAndReimport();
  310. // }
  311. //
  312. // allAddAsset.Add(obj);
  313. }
  314. }
  315. spriteAtlas.SetIncludeInBuild(false);
  316. SpriteAtlasExtensions.Add(spriteAtlas, allAddAsset.ToArray());
  317. EditorUtility.SetDirty(spriteAtlas);
  318. SpriteAtlasUtility.PackAtlases(new SpriteAtlas[] { spriteAtlas }, BuildTarget.Android);
  319. packInfo.lasetJsonGUID = GetFileMD5(Application.dataPath + packInfo.packgJsonPath);
  320. packInfo.isUpdate = false;
  321. string dbjson = JsonUtility.ToJson(UGUICacheInfo.uguiPackDB);
  322. File.WriteAllText(dbPath, dbjson);
  323. }
  324. // UGUIGameObjectTool.ReviewUI(UGUICacheInfo.atlasList, spriteAtlas, packInfo);
  325. }
  326. catch (Exception e)
  327. {
  328. Debug.LogError(e);
  329. }
  330. AssetDatabase.SaveAssets();
  331. AssetDatabase.Refresh();
  332. }
  333. GUILayout.EndHorizontal();
  334. int maxHeight = UGUICacheInfo.uguiPackDB.allPackgInfos.Count * (356) + 1000;
  335. pos = GUI.BeginScrollView(
  336. new Rect(0, 100, EditorGUIUtility.currentViewWidth, this.position.height), pos,
  337. new Rect(0, 0, 10000, maxHeight), false, true);
  338. Rect rect = new Rect(0, 70, 0, 0);
  339. for (int i = 0; i < UGUICacheInfo.uguiPackDB.allPackgInfos.Count; i++)
  340. {
  341. PackInfo packInfo = UGUICacheInfo.uguiPackDB.allPackgInfos[i];
  342. if (!string.IsNullOrEmpty(findName) && !packInfo.packName.Contains(findName))
  343. {
  344. continue;
  345. }
  346. // if ((!packInfo.isUpdate || string.IsNullOrEmpty(packInfo.packgSpritePath)))
  347. // {
  348. // continue;
  349. // }
  350. DrawPack(packInfo, rect);
  351. rect.y += 256 + 100;
  352. }
  353. GUI.EndScrollView();
  354. }
  355. private void DrawPack(PackInfo packInfo, Rect rect)
  356. {
  357. GUI.Box(new Rect(0, rect.y - 20, EditorGUIUtility.currentViewWidth - 180, 300), "");
  358. GUILayout.BeginHorizontal();
  359. float x = 30;
  360. GUI.Label(new Rect(x, rect.y + 10, 200, 30), packInfo.packName);
  361. x += 200;
  362. if (packInfo.isPackError)
  363. {
  364. x += 50;
  365. if (GUI.Button(new Rect(x, rect.y, 50, 30), "校验" + packInfo.packName))
  366. {
  367. packInfo.isPackError = false;
  368. }
  369. }
  370. x += 50;
  371. Rect buttonRect = new Rect(x, rect.y, 50, 50);
  372. if (GUI.Button(buttonRect, "更新"))
  373. {
  374. SpriteAtlas spriteAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(packInfo.packgSpritePath);
  375. if (spriteAtlas == null)
  376. {
  377. return;
  378. }
  379. string json = File.ReadAllText(Application.dataPath + packInfo.packgJsonPath);
  380. TextrueJson textrueJson = JsonUtility.FromJson<TextrueJson>(json);
  381. List<UnityEngine.Object> allAddAsset = new List<UnityEngine.Object>();
  382. UnityEngine.Object[] currObject = spriteAtlas.GetPackables();
  383. SpriteAtlasExtensions.Remove(spriteAtlas, currObject);
  384. spriteAtlas.Remove(currObject);
  385. // spriteAtl = 0;
  386. for (int i = 0; i < textrueJson.newTextureJson.Count; i++)
  387. {
  388. string textrueName = textrueJson.newTextureJson[i].textrueName;
  389. if (textrueName.Contains("bg_"))
  390. {
  391. continue;
  392. }
  393. if (string.IsNullOrEmpty(textrueName))
  394. {
  395. continue;
  396. }
  397. TextrueJson tongYong = UGUICacheInfo.GetTongYong(textrueName);
  398. if (tongYong != null && !tongYong.packName.Equals(packInfo.packName))
  399. {
  400. continue;
  401. }
  402. string[] guidUI = AssetDatabase.FindAssets(textrueName);
  403. if (guidUI != null && guidUI.Length > 0)
  404. {
  405. for (int j = 0; j < guidUI.Length; j++)
  406. {
  407. string path = AssetDatabase.GUIDToAssetPath(guidUI[j]);
  408. if (!path.Contains(".png"))
  409. {
  410. continue;
  411. }
  412. UnityEngine.Object obj = AssetDatabase.LoadMainAssetAtPath(path);
  413. TextureImporter emojiTex = AssetImporter.GetAtPath(path) as TextureImporter;
  414. if (emojiTex != null && emojiTex.textureType != TextureImporterType.Sprite)
  415. {
  416. emojiTex.textureType = TextureImporterType.Sprite;
  417. emojiTex.SaveAndReimport();
  418. }
  419. if (!obj.name.Equals(textrueName))
  420. {
  421. continue;
  422. }
  423. allAddAsset.Add(obj);
  424. break;
  425. }
  426. }
  427. }
  428. spriteAtlas.SetIncludeInBuild(false);
  429. SpriteAtlasAsset spriteAtlasAsset = SpriteAtlasAsset.Load(packInfo.packgSpritePath);
  430. MethodInfo methodInfo = spriteAtlasAsset.GetType()
  431. .GetMethod("RemoveAt", BindingFlags.Instance | BindingFlags.NonPublic);
  432. for (int i = 0; i < spriteAtlas.spriteCount * 3; i++)
  433. {
  434. methodInfo.Invoke(spriteAtlasAsset, new object[] { 0 });
  435. }
  436. // spriteAtlasAsset.Remove(sprites);
  437. spriteAtlasAsset.Add(allAddAsset.ToArray());
  438. SpriteAtlasAsset.Save(spriteAtlasAsset, packInfo.packgSpritePath);
  439. // spriteAtlasAsset.
  440. EditorUtility.SetDirty(spriteAtlas);
  441. AssetDatabase.SaveAssets();
  442. SpriteAtlasUtility.PackAtlases(new SpriteAtlas[] { spriteAtlas }, BuildTarget.NoTarget);
  443. AssetDatabase.Refresh();
  444. packInfo.lasetJsonGUID = GetFileMD5(Application.dataPath + packInfo.packgJsonPath);
  445. packInfo.isUpdate = false;
  446. string dbjson = JsonUtility.ToJson(UGUICacheInfo.uguiPackDB);
  447. File.WriteAllText(dbPath, dbjson);
  448. UGUIGameObjectTool.ReviewUI(UGUICacheInfo.atlasList, spriteAtlas, packInfo);
  449. }
  450. if (string.IsNullOrEmpty(packInfo.packgSpritePath) || packInfo.spriteAtlas == null)
  451. {
  452. x += 50;
  453. buttonRect = new Rect(x, rect.y, 50, 50);
  454. if (GUI.Button(buttonRect, "创建"))
  455. {
  456. string path = "Assets/Res/UIAtlas/";
  457. // string defaultPath = path + "default.spriteatlas";
  458. string assetPath = Application.dataPath.Replace("Assets", "") + path + packInfo.packName +
  459. ".spriteatlasv2";
  460. if (!File.Exists(assetPath))
  461. {
  462. string n = path + "MoBan.spriteatlasv2";
  463. string newP = path + packInfo.packName + ".spriteatlasv2";
  464. AssetDatabase.CopyAsset(n, newP);
  465. // SpriteAtlas spriteAtlas = new SpriteAtlas();
  466. // AssetDatabase.CreateAsset(spriteAtlas, path + packInfo.packName + ".spriteatlasv2");
  467. AssetDatabase.ImportAsset(path + packInfo.packName + ".spriteatlasv2");
  468. AssetDatabase.SaveAssets();
  469. SpriteAtlas spriteAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(newP);
  470. UGUICacheInfo.atlasList.Add(spriteAtlas);
  471. packInfo.spriteAtlas = spriteAtlas;
  472. // AssetDatabase.Refresh();
  473. // File.Copy(Application.dataPath.Replace("Assets", "") + defaultPath, assetPath);
  474. // AssetDatabase.Refresh();
  475. // AssetDatabase.SaveAssets();
  476. }
  477. packInfo.isUpdate = true;
  478. packInfo.packgSpritePath = path + packInfo.packName + ".spriteatlasv2";
  479. string json = JsonUtility.ToJson(UGUICacheInfo.uguiPackDB);
  480. File.WriteAllText(dbPath, json);
  481. }
  482. }
  483. x += 50;
  484. Rect shuaXing = new Rect(x, rect.y, 50, 50);
  485. if (GUI.Button(shuaXing, "刷新"))
  486. {
  487. SpriteAtlas spriteAtlas = AssetDatabase.LoadAssetAtPath<SpriteAtlas>(packInfo.packgSpritePath);
  488. if (spriteAtlas != null)
  489. {
  490. UGUIGameObjectTool.ReviewUI(UGUICacheInfo.atlasList, spriteAtlas, packInfo);
  491. }
  492. }
  493. if (packInfo.packName != null && packInfo.spriteAtlas == null && packInfo.isPackError == false)
  494. {
  495. packInfo.spriteAtlas = UGUICacheInfo.GetSpriteAtlas(packInfo.packName);
  496. if (packInfo.spriteAtlas == null)
  497. {
  498. packInfo.lasetJsonGUID = null;
  499. packInfo.isPackError = true;
  500. }
  501. else
  502. {
  503. packInfo.packgSpritePath = AssetDatabase.GetAssetPath(packInfo.spriteAtlas);
  504. }
  505. UGUICacheInfo.SaveDB();
  506. }
  507. if (packInfo.spriteAtlas != null)
  508. {
  509. x += 50;
  510. EditorGUI.ObjectField(new Rect(x, rect.y, 150, 50), packInfo.spriteAtlas, typeof(SpriteAtlas));
  511. x += 150;
  512. if (GUI.Button(new Rect(x, rect.y, 50, 50), "删除"))
  513. {
  514. AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(packInfo.spriteAtlas));
  515. packInfo.spriteAtlas = null;
  516. AssetDatabase.Refresh();
  517. }
  518. }
  519. GUILayout.EndHorizontal();
  520. // rect.y += 50;
  521. if (packInfo.spriteAtlas != null)
  522. {
  523. Rect scrolViewRect = new Rect(0, rect.y, EditorGUIUtility.currentViewWidth - 200, 256);
  524. Sprite[] sprites = new Sprite[packInfo.spriteAtlas.spriteCount];
  525. packInfo.spriteAtlas.GetSprites(sprites);
  526. List<Texture2D> allTack = sprites.Select(s => s.texture).Distinct().ToList();
  527. packInfo.movePos = GUI.BeginScrollView(scrolViewRect, packInfo.movePos,
  528. new Rect(0, 0, allTack.Count * 356, 250), false, false);
  529. Rect textrueRect = new Rect(30, 0, 256, 256);
  530. for (int i = 0; i < allTack.Count; i++)
  531. {
  532. GUI.DrawTexture(textrueRect, allTack[i], ScaleMode.ScaleToFit);
  533. textrueRect.x += 256 + 100;
  534. }
  535. GUI.EndScrollView();
  536. }
  537. }
  538. private void UpdateArt()
  539. {
  540. string[] scorPath = Directory.GetFiles(UGUICacheInfo.uguiPackDB.uiPath2, "*", SearchOption.AllDirectories);
  541. string[] targetPath =
  542. Directory.GetFiles(Application.dataPath + "/Art/UIAssets", "*", SearchOption.AllDirectories);
  543. //更新资源
  544. UpdateFile(scorPath);
  545. DelectFile(targetPath);
  546. }
  547. private void DelectFile(string[] targetPath)
  548. {
  549. for (int i = 0; i < targetPath.Length; i++)
  550. {
  551. string filePath = targetPath[i].Replace("\\", "/");
  552. filePath = UGUICacheInfo.uguiPackDB.uiPath2 +
  553. filePath.Replace(Application.dataPath + "/Art/UIAssets/", "");
  554. if (filePath.Contains(".meta") || filePath.Contains("TextureSettingConfig") ||
  555. filePath.Contains("dbdata") || filePath.Contains("art.config"))
  556. {
  557. continue;
  558. }
  559. if (!File.Exists(filePath))
  560. {
  561. File.Delete(targetPath[i]);
  562. }
  563. }
  564. AssetDatabase.SaveAssets();
  565. AssetDatabase.Refresh();
  566. }
  567. private void UpdateFile(string[] scorPath)
  568. {
  569. for (int i = 0; i < scorPath.Length; i++)
  570. {
  571. if (scorPath[i].Contains(".svn"))
  572. {
  573. continue;
  574. }
  575. string filePath = scorPath[i].Replace(UGUICacheInfo.uguiPackDB.uiPath2, "");
  576. string scorFilePath = scorPath[i];
  577. string targetFilePath = Application.dataPath + "/Art/UIAssets/" + filePath;
  578. string dirPath = targetFilePath.Replace(Path.GetFileName(scorFilePath), "");
  579. if (!System.IO.Directory.Exists(dirPath))
  580. {
  581. Directory.CreateDirectory(dirPath);
  582. }
  583. if (File.Exists(targetFilePath)) //替换
  584. {
  585. string sucMd5 = GetFileMD5(scorFilePath);
  586. string taggetMd5 = GetFileMD5(targetFilePath);
  587. if (!sucMd5.Equals(taggetMd5)) //图片变化
  588. {
  589. string fileName = Path.GetFileNameWithoutExtension(scorFilePath);
  590. File.Copy(scorFilePath, targetFilePath, true);
  591. AssetDatabase.ImportAsset(targetFilePath.Replace(Application.dataPath, "Assets"),
  592. ImportAssetOptions.ForceUpdate);
  593. TextureImporter emojiTex =
  594. AssetImporter.GetAtPath(targetFilePath.Replace(Application.dataPath, "Assets")) as
  595. TextureImporter;
  596. if (emojiTex != null)
  597. {
  598. emojiTex.textureType = TextureImporterType.Sprite;
  599. emojiTex.SaveAndReimport();
  600. }
  601. }
  602. }
  603. else //新加
  604. {
  605. try
  606. {
  607. if (!scorFilePath.Contains("效果图"))
  608. {
  609. // AssetDatabase.ImportAsset(scorFilePath);
  610. File.Copy(scorFilePath, targetFilePath, true);
  611. AssetDatabase.ImportAsset(targetFilePath.Replace(Application.dataPath, "Assets"),
  612. ImportAssetOptions.ForceUpdate);
  613. TextureImporter emojiTex =
  614. AssetImporter.GetAtPath(targetFilePath.Replace(Application.dataPath, "Assets")) as
  615. TextureImporter;
  616. if (emojiTex != null)
  617. {
  618. emojiTex.textureType = TextureImporterType.Sprite;
  619. emojiTex.SaveAndReimport();
  620. }
  621. }
  622. }
  623. catch (System.Exception e)
  624. {
  625. Debug.LogError(e);
  626. }
  627. }
  628. //string
  629. }
  630. string[] jsonPath = Directory.GetFiles(UGUICacheInfo.uguiPackDB.uiPath2 + "TextrueJson", "*.txt",
  631. SearchOption.AllDirectories);
  632. for (int i = 0; i < jsonPath.Length; i++)
  633. {
  634. string filePath = jsonPath[i].Replace(UGUICacheInfo.uguiPackDB.uiPath2, "");
  635. string scorFilePath = jsonPath[i];
  636. string targetFilePath = Application.dataPath + "/Art/UIAssets/" + filePath;
  637. File.Copy(scorFilePath, targetFilePath, true);
  638. }
  639. AssetDatabase.SaveAssets();
  640. AssetDatabase.Refresh();
  641. }
  642. private string GetFileMD5(string filePath)
  643. {
  644. FileStream file = new FileStream(filePath, System.IO.FileMode.Open);
  645. MD5 md5 = new MD5CryptoServiceProvider();
  646. byte[] retVal = md5.ComputeHash(file);
  647. file.Close();
  648. file.Dispose();
  649. StringBuilder sb = new StringBuilder();
  650. for (int i = 0; i < retVal.Length; i++)
  651. {
  652. sb.Append(retVal[i].ToString("x2"));
  653. }
  654. return sb.ToString();
  655. }
  656. }
  657. }