UGUISpriteAtlasSelectWindow.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.U2D;
  6. using UnityEngine.UI.PackgTool;
  7. namespace UnityEditor.UI
  8. {
  9. public class UGUISpriteAtlasSelectWindow : EditorWindow
  10. {
  11. private static System.Action<SpriteAtlas, Sprite> callBack;
  12. private static string jsonPackPath;
  13. private static List<SpriteAtlas> atlasList = new List<SpriteAtlas>();
  14. private static string spriteAtlasPath = "/Res/UIAtlas";
  15. float mClickTime = 0f;
  16. private static List<Texture> allSprite = new List<Texture>();
  17. private List<SpriteAtlas> currSelectSpriteAtlas = new List<SpriteAtlas>();
  18. private Sprite currSelectSprite;
  19. private static string packName;
  20. private static PackInfo packInfo;
  21. private string findText;
  22. public static void OpenWidow(PackInfo packInfo, System.Action<SpriteAtlas, Sprite> callBack)
  23. {
  24. UGUISpriteAtlasSelectWindow.packInfo = packInfo;
  25. allSprite.Clear();
  26. jsonPackPath = packInfo.PackageJsonPath;
  27. UGUISpriteAtlasSelectWindow.callBack = callBack;
  28. atlasList = UGUICacheInfo.atlasList;
  29. string path = Application.dataPath + UGUISpriteAtlasSelectWindow.jsonPackPath;
  30. List<string> gameAllModel = new List<string>();
  31. string jsonPath = File.ReadAllText(path);
  32. TextrueJson textrueJson = JsonUtility.FromJson<TextrueJson>(jsonPath);
  33. packName = textrueJson.packName;
  34. for (int i = 0; i < textrueJson.newTextureJson.Count; i++)
  35. {
  36. string texturePath = "Assets/Art/UIAssets"+ textrueJson.newTextureJson[i].filePath;
  37. Texture texture = AssetDatabase.LoadAssetAtPath<Texture>(texturePath);
  38. if (texture != null)
  39. {
  40. allSprite.Add(texture);
  41. }
  42. }
  43. UGUISpriteAtlasSelectWindow window = EditorWindow.GetWindow<UGUISpriteAtlasSelectWindow>();
  44. window.currSelectSpriteAtlas.Clear();
  45. window.name = "选择一个一个当前图片";
  46. window.Show();
  47. }
  48. Vector2 pos = Vector2.zero;
  49. private PackInfo GetPackInfo(TextrueJson textrueJson)
  50. {
  51. for (int i = 0; i < UGUICacheInfo.uguiPackDB.allPackgInfos.Count; i++)
  52. {
  53. if (UGUICacheInfo.uguiPackDB.allPackgInfos[i].packName.Equals(textrueJson.packName))
  54. {
  55. return UGUICacheInfo.uguiPackDB.allPackgInfos[i];
  56. }
  57. }
  58. PackInfo packInfo = UGUICacheInfo.AddNewPackInfo(jsonPackPath, textrueJson);
  59. return packInfo;
  60. }
  61. private void GUISpriteAtlas()
  62. {
  63. GUI.DrawTexture(new Rect(EditorGUIUtility.currentViewWidth / 2 - 64, 0, 128, 128), currSelectSprite.texture);
  64. GUILayout.Space(150);
  65. GUILayout.Label($"请选择{currSelectSprite.name}需要使用的图集");
  66. pos = GUILayout.BeginScrollView(pos);
  67. for (int i = 0; i < currSelectSpriteAtlas.Count; i++)
  68. {
  69. if (GUILayout.Button(currSelectSpriteAtlas[i].name))
  70. {
  71. GUILayout.EndScrollView();
  72. callBack?.Invoke(currSelectSpriteAtlas[i], currSelectSprite);
  73. Close();
  74. return;
  75. }
  76. }
  77. GUILayout.EndScrollView();
  78. }
  79. private List<Texture> GetShowTextrue()
  80. {
  81. if (string.IsNullOrEmpty(findText))
  82. {
  83. return allSprite;
  84. }
  85. List<Texture> showSpr = new List<Texture>();
  86. for (int i = 0; i < allSprite.Count; i++)
  87. {
  88. if (allSprite[i].name.Contains(findText))
  89. {
  90. showSpr.Add(allSprite[i]);
  91. }
  92. }
  93. return showSpr;
  94. }
  95. private Sprite GetSprite(Texture t)
  96. {
  97. string p= AssetDatabase.GetAssetPath(t);
  98. return AssetDatabase.LoadAssetAtPath<Sprite>(p);
  99. }
  100. private void GUISprite()
  101. {
  102. findText = GUI.TextField(new Rect(0, 10, 256, 24),findText);
  103. if ( GUI.Button(new Rect(256, 10, 24, 24),"x"))
  104. {
  105. findText = "";
  106. }
  107. float size = 80f;
  108. float padded = size + 10f;
  109. Rect rect = new Rect(10f, 0, size, size);
  110. int screenWidth = (int) EditorGUIUtility.currentViewWidth;
  111. int columns = Mathf.FloorToInt(screenWidth / padded);
  112. if (columns < 1) columns = 1;
  113. GUILayout.Space(50);
  114. pos = GUILayout.BeginScrollView(pos);
  115. int offset = 0;
  116. int rows = 1;
  117. List<Texture> showSpr= GetShowTextrue();
  118. while (offset < showSpr.Count)
  119. {
  120. int col = 0;
  121. rect.x = 10f;
  122. GUILayout.BeginHorizontal();
  123. for (; offset < showSpr.Count; offset++)
  124. {
  125. if (GUI.Button(rect, ""))
  126. {
  127. float delta = Time.realtimeSinceStartup - mClickTime;
  128. mClickTime = Time.realtimeSinceStartup;
  129. if (delta < 0.5f)
  130. {
  131. List<SpriteAtlas> spriteAtlasList = UGUICacheInfo.GetAllSpriteAtlas(showSpr[offset].name);
  132. if (spriteAtlasList.Count > 1)//找到匹配的图集
  133. {
  134. SpriteAtlas currSpriteAtlas = spriteAtlasList[0];
  135. for (int i = 0; i < spriteAtlasList.Count; i++)
  136. {
  137. if (spriteAtlasList[i].name.Equals(packInfo.packName))
  138. {
  139. currSpriteAtlas = spriteAtlasList[i];
  140. break;
  141. }
  142. }
  143. callBack?.Invoke(currSpriteAtlas, GetSprite(showSpr[offset]));
  144. Close();
  145. }
  146. else if (spriteAtlasList.Count == 1)
  147. {
  148. callBack?.Invoke(spriteAtlasList[0], GetSprite(showSpr[offset]));
  149. Close();
  150. }
  151. else
  152. {
  153. callBack?.Invoke(null, GetSprite(showSpr[offset]));
  154. Close();
  155. }
  156. }
  157. }
  158. GUI.DrawTexture(rect, showSpr[offset],ScaleMode.ScaleToFit);
  159. GUI.backgroundColor = new Color(1f, 1f, 1f, 0.5f);
  160. GUI.contentColor = new Color(1f, 1f, 1f, 0.7f);
  161. GUI.Label(new Rect(rect.x, rect.y + rect.height, rect.width, 32f), showSpr[offset].name);
  162. GUI.contentColor = Color.white;
  163. GUI.backgroundColor = Color.white;
  164. rect.x += padded;
  165. if (++col >= columns)
  166. {
  167. ++offset;
  168. break;
  169. }
  170. }
  171. GUILayout.EndHorizontal();
  172. GUILayout.Space(padded);
  173. rect.y += padded + 26;
  174. ++rows;
  175. }
  176. GUILayout.Space(rows * 26);
  177. GUILayout.EndScrollView();
  178. }
  179. void OnGUI()
  180. {
  181. if (currSelectSpriteAtlas.Count > 0)
  182. {
  183. GUISpriteAtlas();
  184. }
  185. else
  186. {
  187. GUISprite();
  188. }
  189. }
  190. }
  191. }