Brak opisu
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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Tilemaps;
  5. using Object = UnityEngine.Object;
  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, 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. Tilemap map = Selection.activeGameObject.GetComponentInParent<Tilemap>();
  85. if (map != null)
  86. map.ClearAllEditorPreviewTiles();
  87. DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
  88. foreach (KeyValuePair<Vector2Int, TileDragAndDropHoverData> item in instance.m_HoverData)
  89. {
  90. Vector3Int gridPos = mouseGridPosition + new Vector3Int(item.Key.x, item.Key.y, 0);
  91. if (item.Value.hoverObject is TileBase)
  92. {
  93. TileBase tile = item.Value.hoverObject as TileBase;
  94. if (map != null)
  95. {
  96. map.SetEditorPreviewTile(gridPos, tile);
  97. }
  98. }
  99. }
  100. }
  101. break;
  102. }
  103. if (instance.m_HoverData != null && (
  104. Event.current.type == EventType.DragExited ||
  105. Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape))
  106. {
  107. if (instance.m_HoverData.Count > 0)
  108. {
  109. Tilemap map = Selection.activeGameObject.GetComponentInParent<Tilemap>();
  110. if (map != null)
  111. map.ClearAllEditorPreviewTiles();
  112. Event.current.Use();
  113. }
  114. instance.m_HoverData = null;
  115. }
  116. }
  117. internal static string GetDefaultTileAssetPath()
  118. {
  119. var path = instance.m_LastUserTileAssetPath;
  120. if (String.IsNullOrEmpty(path))
  121. {
  122. path = ProjectBrowser.s_LastInteractedProjectBrowser != null
  123. ? ProjectBrowser.s_LastInteractedProjectBrowser.GetActiveFolderPath()
  124. : "Assets";
  125. }
  126. return path;
  127. }
  128. internal static void SetUserTileAssetPath(string path)
  129. {
  130. instance.m_LastUserTileAssetPath = path;
  131. }
  132. static Tilemap GetOrCreateActiveTilemap()
  133. {
  134. if (Selection.activeGameObject != null)
  135. {
  136. Tilemap tilemap = Selection.activeGameObject.GetComponentInParent<Tilemap>();
  137. if (tilemap == null)
  138. {
  139. Grid grid = Selection.activeGameObject.GetComponentInParent<Grid>();
  140. tilemap = CreateNewTilemap(grid);
  141. }
  142. return tilemap;
  143. }
  144. return null;
  145. }
  146. static Tilemap CreateNewTilemap(Grid grid)
  147. {
  148. GameObject go = new GameObject("Tilemap");
  149. go.transform.SetParent(grid.gameObject.transform);
  150. Tilemap map = go.AddComponent<Tilemap>();
  151. go.AddComponent<TilemapRenderer>();
  152. return map;
  153. }
  154. static Grid GetActiveGrid()
  155. {
  156. if (Selection.activeGameObject != null)
  157. {
  158. return Selection.activeGameObject.GetComponentInParent<Grid>();
  159. }
  160. return null;
  161. }
  162. }
  163. }