SpriteEditorMenu.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using UnityEngine;
  2. using System;
  3. using UnityEvent = UnityEngine.Event;
  4. namespace UnityEditor.U2D.Sprites
  5. {
  6. [Serializable]
  7. internal class SpriteEditorMenuSetting : ScriptableObject
  8. {
  9. public enum SlicingType { Automatic = 0, GridByCellSize = 1, GridByCellCount = 2 }
  10. [SerializeField]
  11. public Vector2 gridCellCount = new Vector2(1, 1);
  12. [SerializeField]
  13. public Vector2 gridSpriteSize = new Vector2(64, 64);
  14. [SerializeField]
  15. public Vector2 gridSpriteOffset = new Vector2(0, 0);
  16. [SerializeField]
  17. public Vector2 gridSpritePadding = new Vector2(0, 0);
  18. [SerializeField]
  19. public Vector2 pivot = Vector2.zero;
  20. [SerializeField]
  21. public int autoSlicingMethod = (int)SpriteFrameModule.AutoSlicingMethod.DeleteAll;
  22. [SerializeField]
  23. public int spriteAlignment;
  24. [SerializeField]
  25. public SlicingType slicingType;
  26. [SerializeField]
  27. public bool keepEmptyRects;
  28. }
  29. internal class SpriteEditorMenu : EditorWindow
  30. {
  31. private static Styles s_Styles;
  32. private static long s_LastClosedTime;
  33. private static SpriteEditorMenuSetting s_Setting;
  34. private ITextureDataProvider m_TextureDataProvider;
  35. private SpriteFrameModule m_SpriteFrameModule;
  36. private class Styles
  37. {
  38. public GUIStyle background = "grey_border";
  39. public GUIStyle notice;
  40. public Styles()
  41. {
  42. notice = new GUIStyle(GUI.skin.label);
  43. notice.alignment = TextAnchor.MiddleCenter;
  44. notice.wordWrap = true;
  45. }
  46. public readonly GUIContent[] spriteAlignmentOptions =
  47. {
  48. EditorGUIUtility.TrTextContent("Center"),
  49. EditorGUIUtility.TrTextContent("Top Left"),
  50. EditorGUIUtility.TrTextContent("Top"),
  51. EditorGUIUtility.TrTextContent("Top Right"),
  52. EditorGUIUtility.TrTextContent("Left"),
  53. EditorGUIUtility.TrTextContent("Right"),
  54. EditorGUIUtility.TrTextContent("Bottom Left"),
  55. EditorGUIUtility.TrTextContent("Bottom"),
  56. EditorGUIUtility.TrTextContent("Bottom Right"),
  57. EditorGUIUtility.TrTextContent("Custom")
  58. };
  59. public readonly GUIContent[] slicingMethodOptions =
  60. {
  61. EditorGUIUtility.TrTextContent("Delete Existing", "Delete all existing sprite assets before the slicing operation"),
  62. EditorGUIUtility.TrTextContent("Smart", "Try to match existing sprite rects to sliced rects from the slicing operation"),
  63. EditorGUIUtility.TrTextContent("Safe", "Keep existing sprite rects intact")
  64. };
  65. public readonly GUIContent methodLabel = EditorGUIUtility.TrTextContent("Method");
  66. public readonly GUIContent pivotLabel = EditorGUIUtility.TrTextContent("Pivot");
  67. public readonly GUIContent typeLabel = EditorGUIUtility.TrTextContent("Type");
  68. public readonly GUIContent sliceButtonLabel = EditorGUIUtility.TrTextContent("Slice");
  69. public readonly GUIContent columnAndRowLabel = EditorGUIUtility.TrTextContent("Column & Row");
  70. public readonly GUIContent columnLabel = EditorGUIUtility.TextContent("C");
  71. public readonly GUIContent rowLabel = EditorGUIUtility.TextContent("R");
  72. public readonly GUIContent pixelSizeLabel = EditorGUIUtility.TrTextContent("Pixel Size");
  73. public readonly GUIContent xLabel = EditorGUIUtility.TextContent("X");
  74. public readonly GUIContent yLabel = EditorGUIUtility.TextContent("Y");
  75. public readonly GUIContent offsetLabel = EditorGUIUtility.TrTextContent("Offset");
  76. public readonly GUIContent paddingLabel = EditorGUIUtility.TrTextContent("Padding");
  77. public readonly GUIContent automaticSlicingHintLabel = EditorGUIUtility.TrTextContent("To obtain more accurate slicing results, manual slicing is recommended!");
  78. public readonly GUIContent customPivotLabel = EditorGUIUtility.TrTextContent("Custom Pivot");
  79. public readonly GUIContent keepEmptyRectsLabel = EditorGUIUtility.TrTextContent("Keep Empty Rects");
  80. }
  81. private void Init(Rect buttonRect, SpriteFrameModule sf, ITextureDataProvider dataProvider)
  82. {
  83. // Create for once if setting was not created before.
  84. if (s_Setting == null)
  85. s_Setting = CreateInstance<SpriteEditorMenuSetting>();
  86. m_SpriteFrameModule = sf;
  87. m_TextureDataProvider = dataProvider;
  88. buttonRect = GUIUtility.GUIToScreenRect(buttonRect);
  89. float windowHeight = 195;
  90. var windowSize = new Vector2(300, windowHeight);
  91. ShowAsDropDown(buttonRect, windowSize);
  92. Undo.undoRedoPerformed += UndoRedoPerformed;
  93. }
  94. private void UndoRedoPerformed()
  95. {
  96. Repaint();
  97. }
  98. void OnEnable()
  99. {
  100. AssemblyReloadEvents.beforeAssemblyReload += Close;
  101. }
  102. private void OnDisable()
  103. {
  104. AssemblyReloadEvents.beforeAssemblyReload -= Close;
  105. Undo.undoRedoPerformed -= UndoRedoPerformed;
  106. s_LastClosedTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
  107. }
  108. internal static bool ShowAtPosition(Rect buttonRect, SpriteFrameModule sf, ITextureDataProvider textureProvider)
  109. {
  110. // We could not use realtimeSinceStartUp since it is set to 0 when entering/exitting playmode, we assume an increasing time when comparing time.
  111. long nowMilliSeconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
  112. bool justClosed = nowMilliSeconds < s_LastClosedTime + 50;
  113. if (!justClosed)
  114. {
  115. if (UnityEvent.current != null) // Event.current can be null during integration test
  116. UnityEvent.current.Use();
  117. SpriteEditorMenu spriteEditorMenu = CreateInstance<SpriteEditorMenu>();
  118. spriteEditorMenu.Init(buttonRect, sf, textureProvider);
  119. return true;
  120. }
  121. return false;
  122. }
  123. private void OnGUI()
  124. {
  125. if (s_Styles == null)
  126. s_Styles = new Styles();
  127. // Leave some space above the elements
  128. GUILayout.Space(4);
  129. EditorGUIUtility.labelWidth = 124f;
  130. EditorGUIUtility.wideMode = true;
  131. GUI.Label(new Rect(0, 0, position.width, position.height), GUIContent.none, s_Styles.background);
  132. EditorGUI.BeginChangeCheck();
  133. SpriteEditorMenuSetting.SlicingType slicingType = s_Setting.slicingType;
  134. slicingType = (SpriteEditorMenuSetting.SlicingType)EditorGUILayout.EnumPopup(s_Styles.typeLabel, slicingType);
  135. if (EditorGUI.EndChangeCheck())
  136. {
  137. Undo.RegisterCompleteObjectUndo(s_Setting, "Change slicing type");
  138. s_Setting.slicingType = slicingType;
  139. }
  140. switch (slicingType)
  141. {
  142. case SpriteEditorMenuSetting.SlicingType.GridByCellSize:
  143. case SpriteEditorMenuSetting.SlicingType.GridByCellCount:
  144. OnGridGUI();
  145. break;
  146. case SpriteEditorMenuSetting.SlicingType.Automatic:
  147. OnAutomaticGUI();
  148. break;
  149. }
  150. DoPivotGUI();
  151. GUILayout.Space(2f);
  152. EditorGUI.BeginChangeCheck();
  153. int slicingMethod = s_Setting.autoSlicingMethod;
  154. slicingMethod = EditorGUILayout.Popup(s_Styles.methodLabel, slicingMethod, s_Styles.slicingMethodOptions);
  155. if (EditorGUI.EndChangeCheck())
  156. {
  157. Undo.RegisterCompleteObjectUndo(s_Setting, "Change Slicing Method");
  158. s_Setting.autoSlicingMethod = slicingMethod;
  159. }
  160. GUILayout.FlexibleSpace();
  161. GUILayout.BeginHorizontal();
  162. GUILayout.Space(EditorGUIUtility.labelWidth + 4);
  163. if (GUILayout.Button(s_Styles.sliceButtonLabel))
  164. DoSlicing();
  165. GUILayout.EndHorizontal();
  166. }
  167. private void DoSlicing()
  168. {
  169. switch (s_Setting.slicingType)
  170. {
  171. case SpriteEditorMenuSetting.SlicingType.GridByCellCount:
  172. case SpriteEditorMenuSetting.SlicingType.GridByCellSize:
  173. DoGridSlicing();
  174. break;
  175. case SpriteEditorMenuSetting.SlicingType.Automatic:
  176. DoAutomaticSlicing();
  177. break;
  178. }
  179. }
  180. private void TwoIntFields(GUIContent label, GUIContent labelX, GUIContent labelY, ref int x, ref int y)
  181. {
  182. float height = EditorGUI.kSingleLineHeight;
  183. Rect rect = GUILayoutUtility.GetRect(EditorGUILayout.kLabelFloatMinW, EditorGUILayout.kLabelFloatMaxW, height, height, EditorStyles.numberField);
  184. Rect labelRect = rect;
  185. labelRect.width = EditorGUIUtility.labelWidth;
  186. labelRect.height = EditorGUI.kSingleLineHeight;
  187. GUI.Label(labelRect, label);
  188. Rect fieldRect = rect;
  189. fieldRect.width -= EditorGUIUtility.labelWidth;
  190. fieldRect.height = EditorGUI.kSingleLineHeight;
  191. fieldRect.x += EditorGUIUtility.labelWidth;
  192. fieldRect.width /= 2;
  193. fieldRect.width -= 2;
  194. EditorGUIUtility.labelWidth = 12;
  195. x = EditorGUI.IntField(fieldRect, labelX, x);
  196. fieldRect.x += fieldRect.width + 3;
  197. y = EditorGUI.IntField(fieldRect, labelY, y);
  198. EditorGUIUtility.labelWidth = labelRect.width;
  199. }
  200. private void OnGridGUI()
  201. {
  202. int width, height;
  203. m_TextureDataProvider.GetTextureActualWidthAndHeight(out width, out height);
  204. var texture = m_TextureDataProvider.GetReadableTexture2D();
  205. int maxWidth = texture != null ? width : 4096;
  206. int maxHeight = texture != null ? height : 4096;
  207. if (s_Setting.slicingType == SpriteEditorMenuSetting.SlicingType.GridByCellCount)
  208. {
  209. int x = (int)s_Setting.gridCellCount.x;
  210. int y = (int)s_Setting.gridCellCount.y;
  211. EditorGUI.BeginChangeCheck();
  212. TwoIntFields(s_Styles.columnAndRowLabel, s_Styles.columnLabel, s_Styles.rowLabel, ref x, ref y);
  213. if (EditorGUI.EndChangeCheck())
  214. {
  215. Undo.RegisterCompleteObjectUndo(s_Setting, "Change column & row");
  216. s_Setting.gridCellCount.x = Mathf.Clamp(x, 1, maxWidth);
  217. s_Setting.gridCellCount.y = Mathf.Clamp(y, 1, maxHeight);
  218. }
  219. }
  220. else
  221. {
  222. int x = (int)s_Setting.gridSpriteSize.x;
  223. int y = (int)s_Setting.gridSpriteSize.y;
  224. EditorGUI.BeginChangeCheck();
  225. TwoIntFields(s_Styles.pixelSizeLabel, s_Styles.xLabel, s_Styles.yLabel, ref x, ref y);
  226. if (EditorGUI.EndChangeCheck())
  227. {
  228. Undo.RegisterCompleteObjectUndo(s_Setting, "Change grid size");
  229. s_Setting.gridSpriteSize.x = Mathf.Clamp(x, 1, maxWidth);
  230. s_Setting.gridSpriteSize.y = Mathf.Clamp(y, 1, maxHeight);
  231. }
  232. }
  233. {
  234. int x = (int)s_Setting.gridSpriteOffset.x;
  235. int y = (int)s_Setting.gridSpriteOffset.y;
  236. EditorGUI.BeginChangeCheck();
  237. TwoIntFields(s_Styles.offsetLabel, s_Styles.xLabel, s_Styles.yLabel, ref x, ref y);
  238. if (EditorGUI.EndChangeCheck())
  239. {
  240. Undo.RegisterCompleteObjectUndo(s_Setting, "Change grid offset");
  241. s_Setting.gridSpriteOffset.x = Mathf.Clamp(x, 0, maxWidth - s_Setting.gridSpriteSize.x);
  242. s_Setting.gridSpriteOffset.y = Mathf.Clamp(y, 0, maxHeight - s_Setting.gridSpriteSize.y);
  243. }
  244. }
  245. {
  246. int x = (int)s_Setting.gridSpritePadding.x;
  247. int y = (int)s_Setting.gridSpritePadding.y;
  248. EditorGUI.BeginChangeCheck();
  249. TwoIntFields(s_Styles.paddingLabel, s_Styles.xLabel, s_Styles.yLabel, ref x, ref y);
  250. if (EditorGUI.EndChangeCheck())
  251. {
  252. Undo.RegisterCompleteObjectUndo(s_Setting, "Change grid padding");
  253. s_Setting.gridSpritePadding.x = Mathf.Clamp(x, 0, maxWidth);
  254. s_Setting.gridSpritePadding.y = Mathf.Clamp(y, 0, maxHeight);
  255. }
  256. }
  257. EditorGUI.BeginChangeCheck();
  258. bool keepEmptyRects = s_Setting.keepEmptyRects;
  259. keepEmptyRects = EditorGUILayout.Toggle(s_Styles.keepEmptyRectsLabel, keepEmptyRects);
  260. if (EditorGUI.EndChangeCheck())
  261. {
  262. Undo.RegisterCompleteObjectUndo(s_Setting, "Keep Empty Rects");
  263. s_Setting.keepEmptyRects = keepEmptyRects;
  264. }
  265. }
  266. private void OnAutomaticGUI()
  267. {
  268. float spacing = 38f;
  269. var texture = m_TextureDataProvider.GetReadableTexture2D();
  270. if (texture != null && UnityEditor.TextureUtil.IsCompressedTextureFormat(texture.format))
  271. {
  272. EditorGUILayout.LabelField(s_Styles.automaticSlicingHintLabel, s_Styles.notice);
  273. spacing -= 31f;
  274. }
  275. }
  276. private void DoPivotGUI()
  277. {
  278. EditorGUI.BeginChangeCheck();
  279. int alignment = s_Setting.spriteAlignment;
  280. alignment = EditorGUILayout.Popup(s_Styles.pivotLabel, alignment, s_Styles.spriteAlignmentOptions);
  281. if (EditorGUI.EndChangeCheck())
  282. {
  283. Undo.RegisterCompleteObjectUndo(s_Setting, "Change Alignment");
  284. s_Setting.spriteAlignment = alignment;
  285. s_Setting.pivot = SpriteEditorUtility.GetPivotValue((SpriteAlignment)alignment, s_Setting.pivot);
  286. }
  287. Vector2 pivot = s_Setting.pivot;
  288. EditorGUI.BeginChangeCheck();
  289. using (new EditorGUI.DisabledScope(alignment != (int)SpriteAlignment.Custom))
  290. {
  291. pivot = EditorGUILayout.Vector2Field(s_Styles.customPivotLabel, pivot);
  292. }
  293. if (EditorGUI.EndChangeCheck())
  294. {
  295. Undo.RegisterCompleteObjectUndo(s_Setting, "Change custom pivot");
  296. s_Setting.pivot = pivot;
  297. }
  298. }
  299. private void DoAutomaticSlicing()
  300. {
  301. // 4 seems to be a pretty nice min size for a automatic sprite slicing. It used to be exposed to the slicing dialog, but it is actually better workflow to slice&crop manually than find a suitable size number
  302. m_SpriteFrameModule.DoAutomaticSlicing(4, s_Setting.spriteAlignment, s_Setting.pivot, (SpriteFrameModule.AutoSlicingMethod)s_Setting.autoSlicingMethod);
  303. }
  304. private void DoGridSlicing()
  305. {
  306. if (s_Setting.slicingType == SpriteEditorMenuSetting.SlicingType.GridByCellCount)
  307. DetemineGridCellSizeWithCellCount();
  308. m_SpriteFrameModule.DoGridSlicing(s_Setting.gridSpriteSize, s_Setting.gridSpriteOffset, s_Setting.gridSpritePadding, s_Setting.spriteAlignment, s_Setting.pivot, (SpriteFrameModule.AutoSlicingMethod)s_Setting.autoSlicingMethod, s_Setting.keepEmptyRects);
  309. }
  310. private void DetemineGridCellSizeWithCellCount()
  311. {
  312. int width, height;
  313. m_TextureDataProvider.GetTextureActualWidthAndHeight(out width, out height);
  314. var texture = m_TextureDataProvider.GetReadableTexture2D();
  315. int maxWidth = texture != null ? width : 4096;
  316. int maxHeight = texture != null ? height : 4096;
  317. s_Setting.gridSpriteSize.x = (maxWidth - s_Setting.gridSpriteOffset.x - (s_Setting.gridSpritePadding.x * s_Setting.gridCellCount.x)) / s_Setting.gridCellCount.x;
  318. s_Setting.gridSpriteSize.y = (maxHeight - s_Setting.gridSpriteOffset.y - (s_Setting.gridSpritePadding.y * s_Setting.gridCellCount.y)) / s_Setting.gridCellCount.y;
  319. s_Setting.gridSpriteSize.x = Mathf.Clamp(s_Setting.gridSpriteSize.x, 1, maxWidth);
  320. s_Setting.gridSpriteSize.y = Mathf.Clamp(s_Setting.gridSpriteSize.y, 1, maxHeight);
  321. }
  322. }
  323. }