UGUIPackgToolWindow.cs 30 KB

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