TileDragAndDropManager.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Tilemaps;
  4. using Object = UnityEngine.Object;
  5. namespace UnityEditor.Tilemaps
  6. {
  7. /// <summary> This class is in charge of drag'n'drops of Tile assets on scene view </summary>
  8. internal class TileDragAndDropManager : ScriptableSingleton<TileDragAndDropManager>
  9. {
  10. private bool m_RegisteredEventHandlers;
  11. private Dictionary<Vector2Int, Object> m_HoverData;
  12. [InitializeOnLoadMethod]
  13. static void Initialize()
  14. {
  15. instance.RegisterEventHandlers();
  16. }
  17. void OnEnable()
  18. {
  19. RegisterEventHandlers();
  20. }
  21. void RegisterEventHandlers()
  22. {
  23. if (m_RegisteredEventHandlers)
  24. return;
  25. SceneView.duringSceneGui += DuringSceneGui;
  26. m_RegisteredEventHandlers = true;
  27. }
  28. void OnDisable()
  29. {
  30. SceneView.duringSceneGui -= DuringSceneGui;
  31. m_RegisteredEventHandlers = false;
  32. }
  33. private void DuringSceneGui(SceneView sceneView)
  34. {
  35. Event evt = Event.current;
  36. if (evt.type != EventType.DragUpdated && evt.type != EventType.DragPerform && evt.type != EventType.DragExited && evt.type != EventType.Repaint)
  37. return;
  38. Grid activeGrid = GetActiveGrid();
  39. if (activeGrid == null || DragAndDrop.objectReferences.Length == 0)
  40. return;
  41. Vector3 localMouse = GridEditorUtility.ScreenToLocal(activeGrid.transform, evt.mousePosition);
  42. Vector3Int mouseGridPosition = activeGrid.LocalToCell(localMouse);
  43. switch (evt.type)
  44. {
  45. //TODO: Cache this
  46. case EventType.DragUpdated:
  47. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  48. List<TileBase> tiles = TileDragAndDrop.GetValidTiles(DragAndDrop.objectReferences);
  49. instance.m_HoverData = TileDragAndDrop.CreateHoverData(null, null, tiles);
  50. if (instance.m_HoverData.Count > 0)
  51. {
  52. Event.current.Use();
  53. GUI.changed = true;
  54. }
  55. break;
  56. case EventType.DragPerform:
  57. if (instance.m_HoverData.Count > 0)
  58. {
  59. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  60. Dictionary<Vector2Int, TileBase> tileSheet = TileDragAndDrop.ConvertToTileSheet(instance.m_HoverData);
  61. Tilemap tilemap = GetOrCreateActiveTilemap();
  62. tilemap.ClearAllEditorPreviewTiles();
  63. foreach (KeyValuePair<Vector2Int, TileBase> item in tileSheet)
  64. {
  65. Vector3Int position = new Vector3Int(mouseGridPosition.x + item.Key.x, mouseGridPosition.y + item.Key.y, 0);
  66. tilemap.SetTile(position, item.Value);
  67. }
  68. instance.m_HoverData = null;
  69. GUI.changed = true;
  70. Event.current.Use();
  71. }
  72. break;
  73. case EventType.Repaint:
  74. if (instance.m_HoverData != null)
  75. {
  76. Tilemap map = Selection.activeGameObject.GetComponentInParent<Tilemap>();
  77. if (map != null)
  78. map.ClearAllEditorPreviewTiles();
  79. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  80. foreach (KeyValuePair<Vector2Int, Object> item in instance.m_HoverData)
  81. {
  82. Vector3Int gridPos = mouseGridPosition + new Vector3Int(item.Key.x, item.Key.y, 0);
  83. if (item.Value is TileBase)
  84. {
  85. TileBase tile = item.Value as TileBase;
  86. if (map != null)
  87. {
  88. map.SetEditorPreviewTile(gridPos, tile);
  89. }
  90. }
  91. }
  92. }
  93. break;
  94. }
  95. if (instance.m_HoverData != null && (
  96. Event.current.type == EventType.DragExited ||
  97. Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape))
  98. {
  99. if (instance.m_HoverData.Count > 0)
  100. {
  101. Tilemap map = Selection.activeGameObject.GetComponentInParent<Tilemap>();
  102. if (map != null)
  103. map.ClearAllEditorPreviewTiles();
  104. Event.current.Use();
  105. }
  106. instance.m_HoverData = null;
  107. }
  108. }
  109. static Tilemap GetOrCreateActiveTilemap()
  110. {
  111. if (Selection.activeGameObject != null)
  112. {
  113. Tilemap tilemap = Selection.activeGameObject.GetComponentInParent<Tilemap>();
  114. if (tilemap == null)
  115. {
  116. Grid grid = Selection.activeGameObject.GetComponentInParent<Grid>();
  117. tilemap = CreateNewTilemap(grid);
  118. }
  119. return tilemap;
  120. }
  121. return null;
  122. }
  123. static Tilemap CreateNewTilemap(Grid grid)
  124. {
  125. GameObject go = new GameObject("Tilemap");
  126. go.transform.SetParent(grid.gameObject.transform);
  127. Tilemap map = go.AddComponent<Tilemap>();
  128. go.AddComponent<TilemapRenderer>();
  129. return map;
  130. }
  131. public static Grid GetActiveGrid()
  132. {
  133. if (Selection.activeGameObject != null)
  134. {
  135. return Selection.activeGameObject.GetComponentInParent<Grid>();
  136. }
  137. return null;
  138. }
  139. }
  140. }