No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnimatedTileEditor.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using UnityEditorInternal;
  3. using UnityEngine.Scripting.APIUpdating;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using UnityEngine;
  7. using UnityEngine.Tilemaps;
  8. using Object = UnityEngine.Object;
  9. namespace UnityEditor.Tilemaps
  10. {
  11. [CustomEditor(typeof(AnimatedTile))]
  12. [MovedFrom(true, "UnityEngine.Tilemaps", "Unity.2D.Tilemap.Extras")]
  13. public class AnimatedTileEditor : Editor
  14. {
  15. private static class Styles
  16. {
  17. public static readonly GUIContent orderAnimatedTileSpritesInfo =
  18. EditorGUIUtility.TrTextContent("Place sprites shown based on the order of animation.");
  19. public static readonly GUIContent emptyAnimatedTileInfo =
  20. EditorGUIUtility.TrTextContent(
  21. "Drag Sprite or Sprite Texture assets \n" +
  22. " to start creating an Animated Tile.");
  23. public static readonly GUIContent minimumSpeedLabel = EditorGUIUtility.TrTextContent("Minimum Speed",
  24. "The minimum possible speed at which the Animation of the Tile will be played. A speed value will be randomly chosen between the minimum and maximum speed.");
  25. public static readonly GUIContent maximumSpeedLabel = EditorGUIUtility.TrTextContent("Maximum Speed",
  26. "The maximum possible speed at which the Animation of the Tile will be played. A speed value will be randomly chosen between the minimum and maximum speed.");
  27. public static readonly GUIContent startTimeLabel = EditorGUIUtility.TrTextContent("Start Time", "The starting time of this Animated Tile. This allows you to start the Animation from a particular time.");
  28. public static readonly GUIContent startFrameLabel = EditorGUIUtility.TrTextContent("Start Frame", "The starting frame of this Animated Tile. This allows you to start the Animation from a particular Sprite in the list of Animated Sprites.");
  29. public static readonly GUIContent colliderTypeLabel = EditorGUIUtility.TrTextContent("Collider Type", "The Collider Shape generated by the Tile.");
  30. public static readonly GUIContent flagsLabel = EditorGUIUtility.TrTextContent("Flags", "Flags for controlling the Tile Animation.");
  31. }
  32. private static readonly string k_UndoName = L10n.Tr("Change AnimatedTile");
  33. private SerializedProperty m_AnimatedSprites;
  34. private AnimatedTile tile { get { return (target as AnimatedTile); } }
  35. private List<Sprite> dragAndDropSprites;
  36. private ReorderableList reorderableList;
  37. private void OnEnable()
  38. {
  39. reorderableList = new ReorderableList(tile.m_AnimatedSprites, typeof(Sprite), true, true, true, true);
  40. reorderableList.drawHeaderCallback = OnDrawHeader;
  41. reorderableList.drawElementCallback = OnDrawElement;
  42. reorderableList.elementHeightCallback = GetElementHeight;
  43. reorderableList.onAddCallback = OnAddElement;
  44. reorderableList.onRemoveCallback = OnRemoveElement;
  45. reorderableList.onReorderCallback = OnReorderElement;
  46. m_AnimatedSprites = serializedObject.FindProperty("m_AnimatedSprites");
  47. }
  48. private void OnDrawHeader(Rect rect)
  49. {
  50. GUI.Label(rect, Styles.orderAnimatedTileSpritesInfo);
  51. }
  52. private void OnDrawElement(Rect rect, int index, bool isActive, bool isFocused)
  53. {
  54. if (tile.m_AnimatedSprites != null && index < tile.m_AnimatedSprites.Length)
  55. {
  56. var spriteName = tile.m_AnimatedSprites[index] != null ? tile.m_AnimatedSprites[index].name : "Null";
  57. tile.m_AnimatedSprites[index] = (Sprite) EditorGUI.ObjectField(rect
  58. , $"Sprite {index + 1}: {spriteName}"
  59. , tile.m_AnimatedSprites[index]
  60. , typeof(Sprite)
  61. , false);
  62. }
  63. }
  64. private float GetElementHeight(int index)
  65. {
  66. return 3 * EditorGUI.GetPropertyHeight(SerializedPropertyType.ObjectReference,
  67. null);
  68. }
  69. private void OnAddElement(ReorderableList list)
  70. {
  71. var count = tile.m_AnimatedSprites != null ? tile.m_AnimatedSprites.Length + 1 : 1;
  72. ResizeAnimatedSpriteList(count);
  73. if (list.index == 0 || list.index < list.count)
  74. {
  75. Array.Copy(tile.m_AnimatedSprites, list.index + 1, tile.m_AnimatedSprites, list.index + 2, list.count - list.index - 1);
  76. tile.m_AnimatedSprites[list.index + 1] = null;
  77. if (list.IsSelected(list.index))
  78. list.index += 1;
  79. }
  80. else
  81. {
  82. tile.m_AnimatedSprites[count - 1] = null;
  83. }
  84. }
  85. private void OnRemoveElement(ReorderableList list)
  86. {
  87. if (tile.m_AnimatedSprites != null && tile.m_AnimatedSprites.Length > 0 && list.index < tile.m_AnimatedSprites.Length)
  88. {
  89. var sprites = tile.m_AnimatedSprites.ToList();
  90. sprites.RemoveAt(list.index);
  91. tile.m_AnimatedSprites = sprites.ToArray();
  92. }
  93. }
  94. private void OnReorderElement(ReorderableList list)
  95. {
  96. // Fix for 2020.1, which does not track changes when reordering in the list
  97. EditorUtility.SetDirty(tile);
  98. }
  99. private void DisplayClipboardText(GUIContent clipboardText, Rect position)
  100. {
  101. Color old = GUI.color;
  102. GUI.color = Color.gray;
  103. var infoSize = GUI.skin.label.CalcSize(clipboardText);
  104. Rect rect = new Rect(position.center.x - infoSize.x * .5f
  105. , position.center.y - infoSize.y * .5f
  106. , infoSize.x
  107. , infoSize.y);
  108. GUI.Label(rect, clipboardText);
  109. GUI.color = old;
  110. }
  111. private bool dragAndDropActive
  112. {
  113. get
  114. {
  115. return dragAndDropSprites != null
  116. && dragAndDropSprites.Count > 0;
  117. }
  118. }
  119. private void DragAndDropClear()
  120. {
  121. dragAndDropSprites = null;
  122. DragAndDrop.visualMode = DragAndDropVisualMode.None;
  123. Event.current.Use();
  124. }
  125. private static List<Sprite> GetSpritesFromTexture(Texture2D texture)
  126. {
  127. string path = AssetDatabase.GetAssetPath(texture);
  128. Object[] assets = AssetDatabase.LoadAllAssetsAtPath(path);
  129. List<Sprite> sprites = new List<Sprite>();
  130. foreach (Object asset in assets)
  131. {
  132. if (asset is Sprite)
  133. {
  134. sprites.Add(asset as Sprite);
  135. }
  136. }
  137. return sprites;
  138. }
  139. private static List<Sprite> GetValidSingleSprites(Object[] objects)
  140. {
  141. List<Sprite> result = new List<Sprite>();
  142. foreach (Object obj in objects)
  143. {
  144. if (obj is Sprite)
  145. {
  146. result.Add(obj as Sprite);
  147. }
  148. else if (obj is Texture2D)
  149. {
  150. Texture2D texture = obj as Texture2D;
  151. List<Sprite> sprites = GetSpritesFromTexture(texture);
  152. if (sprites.Count > 0)
  153. {
  154. result.AddRange(sprites);
  155. }
  156. }
  157. }
  158. return result;
  159. }
  160. private void HandleDragAndDrop(Rect guiRect)
  161. {
  162. if (DragAndDrop.objectReferences.Length == 0 || !guiRect.Contains(Event.current.mousePosition))
  163. return;
  164. switch (Event.current.type)
  165. {
  166. case EventType.DragUpdated:
  167. {
  168. dragAndDropSprites = GetValidSingleSprites(DragAndDrop.objectReferences);
  169. if (dragAndDropActive)
  170. {
  171. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  172. Event.current.Use();
  173. GUI.changed = true;
  174. }
  175. }
  176. break;
  177. case EventType.DragPerform:
  178. {
  179. if (!dragAndDropActive)
  180. return;
  181. Undo.RegisterCompleteObjectUndo(tile, "Drag and Drop to Animated Tile");
  182. ResizeAnimatedSpriteList(dragAndDropSprites.Count);
  183. Array.Copy(dragAndDropSprites.ToArray(), tile.m_AnimatedSprites, dragAndDropSprites.Count);
  184. DragAndDropClear();
  185. GUI.changed = true;
  186. EditorUtility.SetDirty(tile);
  187. GUIUtility.ExitGUI();
  188. }
  189. break;
  190. case EventType.Repaint:
  191. // Handled in Render()
  192. break;
  193. }
  194. if (Event.current.type == EventType.DragExited ||
  195. Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape)
  196. {
  197. DragAndDropClear();
  198. }
  199. }
  200. /// <summary>
  201. /// Draws an Inspector for the AnimatedTile.
  202. /// </summary>
  203. public override void OnInspectorGUI()
  204. {
  205. serializedObject.Update();
  206. Undo.RecordObject(tile, k_UndoName);
  207. EditorGUI.BeginChangeCheck();
  208. int count = EditorGUILayout.DelayedIntField("Number of Animated Sprites", tile.m_AnimatedSprites != null ? tile.m_AnimatedSprites.Length : 0);
  209. if (count < 0)
  210. count = 0;
  211. if (tile.m_AnimatedSprites == null || tile.m_AnimatedSprites.Length != count)
  212. ResizeAnimatedSpriteList(count);
  213. if (count == 0)
  214. {
  215. Rect rect = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight * 5);
  216. HandleDragAndDrop(rect);
  217. EditorGUI.DrawRect(rect, dragAndDropActive && rect.Contains(Event.current.mousePosition) ? Color.white : Color.black);
  218. var innerRect = new Rect(rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2);
  219. EditorGUI.DrawRect(innerRect, EditorGUIUtility.isProSkin
  220. ? (Color) new Color32 (56, 56, 56, 255)
  221. : (Color) new Color32 (194, 194, 194, 255));
  222. DisplayClipboardText(Styles.emptyAnimatedTileInfo, rect);
  223. GUILayout.Space(rect.height);
  224. EditorGUILayout.Space();
  225. }
  226. if (reorderableList != null)
  227. {
  228. var tileCount = tile.m_AnimatedSprites != null ? tile.m_AnimatedSprites.Length : 0;
  229. if (reorderableList.list == null || reorderableList.count != tileCount)
  230. reorderableList.list = tile.m_AnimatedSprites;
  231. reorderableList.DoLayoutList();
  232. }
  233. using (new EditorGUI.DisabledScope(tile.m_AnimatedSprites == null || tile.m_AnimatedSprites.Length == 0))
  234. {
  235. float minSpeed = EditorGUILayout.FloatField(Styles.minimumSpeedLabel, tile.m_MinSpeed);
  236. float maxSpeed = EditorGUILayout.FloatField(Styles.maximumSpeedLabel, tile.m_MaxSpeed);
  237. if (minSpeed < 0.0f)
  238. minSpeed = 0.0f;
  239. if (maxSpeed < 0.0f)
  240. maxSpeed = 0.0f;
  241. if (maxSpeed < minSpeed)
  242. maxSpeed = minSpeed;
  243. tile.m_MinSpeed = minSpeed;
  244. tile.m_MaxSpeed = maxSpeed;
  245. using (new EditorGUI.DisabledScope(tile.m_AnimatedSprites == null
  246. || (0 < tile.m_AnimationStartFrame
  247. && tile.m_AnimationStartFrame <= tile.m_AnimatedSprites.Length)))
  248. {
  249. tile.m_AnimationStartTime = EditorGUILayout.FloatField(Styles.startTimeLabel, tile.m_AnimationStartTime);
  250. }
  251. tile.m_AnimationStartFrame = EditorGUILayout.IntField(Styles.startFrameLabel, tile.m_AnimationStartFrame);
  252. tile.m_TileColliderType = (Tile.ColliderType) EditorGUILayout.EnumPopup(Styles.colliderTypeLabel, tile.m_TileColliderType);
  253. #if UNITY_2022_2_OR_NEWER
  254. tile.m_TileAnimationFlags = (TileAnimationFlags) EditorGUILayout.EnumFlagsField(Styles.flagsLabel, tile.m_TileAnimationFlags);
  255. #endif
  256. }
  257. if (EditorGUI.EndChangeCheck())
  258. {
  259. serializedObject.ApplyModifiedProperties();
  260. EditorUtility.SetDirty(tile);
  261. }
  262. }
  263. private void ResizeAnimatedSpriteList(int count)
  264. {
  265. m_AnimatedSprites.arraySize = count;
  266. serializedObject.ApplyModifiedProperties();
  267. }
  268. }
  269. }