Sin descripción
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.

TileDragAndDropManager.cs 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. using UnityEngine.Tilemaps;
  6. namespace UnityEditor.Tilemaps
  7. {
  8. /// <summary> This class is in charge of drag'n'drops of Tile assets on scene view </summary>
  9. internal class TileDragAndDropManager : ScriptableSingleton<TileDragAndDropManager>
  10. {
  11. private bool m_RegisteredEventHandlers;
  12. private Dictionary<Vector2Int, TileDragAndDropHoverData> m_HoverData;
  13. [SerializeField]
  14. private string m_LastUserTileAssetPath;
  15. [InitializeOnLoadMethod]
  16. static void Initialize()
  17. {
  18. instance.RegisterEventHandlers();
  19. }
  20. void OnEnable()
  21. {
  22. RegisterEventHandlers();
  23. }
  24. void RegisterEventHandlers()
  25. {
  26. if (m_RegisteredEventHandlers)
  27. return;
  28. SceneView.duringSceneGui += DuringSceneGui;
  29. m_RegisteredEventHandlers = true;
  30. }
  31. void OnDisable()
  32. {
  33. SceneView.duringSceneGui -= DuringSceneGui;
  34. m_RegisteredEventHandlers = false;
  35. }
  36. private void DuringSceneGui(SceneView sceneView)
  37. {
  38. Event evt = Event.current;
  39. if (evt.type != EventType.DragUpdated && evt.type != EventType.DragPerform && evt.type != EventType.DragExited && evt.type != EventType.Repaint)
  40. return;
  41. Grid activeGrid = GetActiveGrid();
  42. if (activeGrid == null || DragAndDrop.objectReferences.Length == 0)
  43. return;
  44. Vector3 localMouse = GridEditorUtility.ScreenToLocal(activeGrid.transform, evt.mousePosition);
  45. Vector3Int mouseGridPosition = activeGrid.LocalToCell(localMouse);
  46. switch (evt.type)
  47. {
  48. //TODO: Cache this
  49. case EventType.DragUpdated:
  50. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  51. List<TileBase> tiles = TileDragAndDrop.GetValidTiles(DragAndDrop.objectReferences);
  52. instance.m_HoverData = TileDragAndDrop.CreateHoverData(null, null, tiles, null, activeGrid.cellLayout);
  53. if (instance.m_HoverData.Count > 0)
  54. {
  55. Event.current.Use();
  56. GUI.changed = true;
  57. }
  58. break;
  59. case EventType.DragPerform:
  60. if (instance.m_HoverData.Count > 0)
  61. {
  62. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  63. var tileSheet = TileDragAndDrop.ConvertToTileSheet(instance.m_HoverData);
  64. Tilemap tilemap = GetOrCreateActiveTilemap();
  65. tilemap.ClearAllEditorPreviewTiles();
  66. int i = 0;
  67. foreach (KeyValuePair<Vector2Int, TileDragAndDropHoverData> item in instance.m_HoverData)
  68. {
  69. Vector3Int position = new Vector3Int(mouseGridPosition.x + item.Key.x, mouseGridPosition.y + item.Key.y, 0);
  70. tilemap.SetTile(position, tileSheet[i++]);
  71. tilemap.SetTransformMatrix(position, Matrix4x4.TRS(
  72. item.Value.hasOffset ? item.Value.positionOffset - tilemap.tileAnchor : Vector3.zero
  73. , Quaternion.identity
  74. , Vector3.one));
  75. }
  76. instance.m_HoverData = null;
  77. GUI.changed = true;
  78. Event.current.Use();
  79. }
  80. break;
  81. case EventType.Repaint:
  82. if (instance.m_HoverData != null)
  83. {
  84. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  85. var map = Selection.activeGameObject.GetComponentInParent<Tilemap>();
  86. var hasMap = map != null;
  87. if (hasMap)
  88. {
  89. map.ClearAllEditorPreviewTiles();
  90. foreach (KeyValuePair<Vector2Int, TileDragAndDropHoverData> item in instance.m_HoverData)
  91. {
  92. var gridPos = mouseGridPosition + new Vector3Int(item.Key.x, item.Key.y, 0);
  93. if (item.Value.hoverObject is TileBase tile)
  94. {
  95. map.SetEditorPreviewTile(gridPos, tile);
  96. }
  97. }
  98. }
  99. }
  100. break;
  101. }
  102. if (instance.m_HoverData != null && (
  103. Event.current.type == EventType.DragExited ||
  104. Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape))
  105. {
  106. if (instance.m_HoverData.Count > 0)
  107. {
  108. Tilemap map = Selection.activeGameObject.GetComponentInParent<Tilemap>();
  109. if (map != null)
  110. map.ClearAllEditorPreviewTiles();
  111. Event.current.Use();
  112. }
  113. instance.m_HoverData = null;
  114. }
  115. }
  116. internal static string GetDefaultTileAssetDirectoryPath()
  117. {
  118. var path = instance.m_LastUserTileAssetPath;
  119. if (String.IsNullOrEmpty(path))
  120. {
  121. path = ProjectBrowser.s_LastInteractedProjectBrowser != null
  122. ? ProjectBrowser.s_LastInteractedProjectBrowser.GetActiveFolderPath()
  123. : "Assets";
  124. }
  125. return path;
  126. }
  127. internal static void SetUserTileAssetDirectoryPath(string path)
  128. {
  129. var directoryPath = String.Empty;
  130. if (!String.IsNullOrEmpty(path))
  131. {
  132. // UUM-29240: UnityGetDirectoryName clips off last directory if path is not a file path
  133. if (String.IsNullOrEmpty(FileUtil.GetPathExtension(path))
  134. && !path.EndsWith("/") // MacOS
  135. && !File.Exists(path))
  136. {
  137. path = FileUtil.CombinePaths(path, "");
  138. }
  139. directoryPath = FileUtil.UnityGetDirectoryName(path);
  140. }
  141. instance.m_LastUserTileAssetPath = directoryPath;
  142. }
  143. static Tilemap GetOrCreateActiveTilemap()
  144. {
  145. if (Selection.activeGameObject != null)
  146. {
  147. Tilemap tilemap = Selection.activeGameObject.GetComponentInParent<Tilemap>();
  148. if (tilemap == null)
  149. {
  150. Grid grid = Selection.activeGameObject.GetComponentInParent<Grid>();
  151. tilemap = CreateNewTilemap(grid);
  152. }
  153. return tilemap;
  154. }
  155. return null;
  156. }
  157. static Tilemap CreateNewTilemap(Grid grid)
  158. {
  159. GameObject go = new GameObject("Tilemap");
  160. go.transform.SetParent(grid.gameObject.transform);
  161. Tilemap map = go.AddComponent<Tilemap>();
  162. go.AddComponent<TilemapRenderer>();
  163. return map;
  164. }
  165. static Grid GetActiveGrid()
  166. {
  167. if (Selection.activeGameObject != null)
  168. {
  169. return Selection.activeGameObject.GetComponentInParent<Grid>();
  170. }
  171. return null;
  172. }
  173. }
  174. }