public void HandleDragAndDrop()
        {
            if (DragAndDrop.objectReferences.Length == 0 || !guiRect.Contains(Event.current.mousePosition))
            {
                return;
            }

            switch (Event.current.type)
            {
            //TODO: Cache this
            case EventType.DragUpdated:
            {
                List <Texture2D> sheets  = TileDragAndDrop.GetValidSpritesheets(DragAndDrop.objectReferences);
                List <Sprite>    sprites = TileDragAndDrop.GetValidSingleSprites(DragAndDrop.objectReferences);
                List <TileBase>  tiles   = TileDragAndDrop.GetValidTiles(DragAndDrop.objectReferences);
                m_HoverData = TileDragAndDrop.CreateHoverData(sheets, sprites, tiles);

                if (m_HoverData != null && m_HoverData.Count > 0)
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                    Event.current.Use();
                    GUI.changed = true;
                }
            }
            break;

            case EventType.DragPerform:
            {
                if (m_HoverData == null || m_HoverData.Count == 0)
                {
                    return;
                }

                RegisterUndo();

                bool wasEmpty = TilemapIsEmpty(tilemap);

                Vector2Int targetPosition = mouseGridPosition;
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                Dictionary <Vector2Int, TileBase> tileSheet = TileDragAndDrop.ConvertToTileSheet(m_HoverData);
                foreach (KeyValuePair <Vector2Int, TileBase> item in tileSheet)
                {
                    SetTile(tilemap, targetPosition + item.Key, item.Value, Color.white, Matrix4x4.identity);
                }

                OnPaletteChanged();

                m_PaletteNeedsSave = true;
                FlushHoverData();
                GUI.changed = true;
                SavePaletteIfNecessary();

                if (wasEmpty)
                {
                    ResetPreviewInstance();
                    FrameEntirePalette();
                }

                Event.current.Use();
                GUIUtility.ExitGUI();
            }
            break;

            case EventType.Repaint:
                // Handled in Render()
                break;
            }

            if (m_HoverData != null && (
                    Event.current.type == EventType.DragExited ||
                    Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape))
            {
                DragAndDrop.visualMode = DragAndDropVisualMode.None;
                FlushHoverData();
                Event.current.Use();
            }
        }
        public void HandleDragAndDrop()
        {
            if (DragAndDrop.objectReferences.Length == 0 || !guiRect.Contains(Event.current.mousePosition))
            {
                return;
            }

            switch (Event.current.type)
            {
            //TODO: Cache this
            case EventType.DragUpdated:
            {
                List <Texture2D> sheets  = TileDragAndDrop.GetValidSpritesheets(DragAndDrop.objectReferences);
                List <Sprite>    sprites = TileDragAndDrop.GetValidSingleSprites(DragAndDrop.objectReferences);
                List <TileBase>  tiles   = TileDragAndDrop.GetValidTiles(DragAndDrop.objectReferences);
                m_HoverData = TileDragAndDrop.CreateHoverData(sheets, sprites, tiles, tilemap.cellLayout);

                if (m_HoverData != null && m_HoverData.Count > 0)
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                    Event.current.Use();
                    GUI.changed = true;
                }
            }
            break;

            case EventType.DragPerform:
            {
                if (m_HoverData == null || m_HoverData.Count == 0)
                {
                    return;
                }

                RegisterUndo();

                bool wasEmpty = TilemapIsEmpty(tilemap);

                Vector2Int targetPosition = mouseGridPosition;
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
                var tileSheet = TileDragAndDrop.ConvertToTileSheet(m_HoverData);
                int i         = 0;
                foreach (KeyValuePair <Vector2Int, TileDragAndDropHoverData> item in m_HoverData)
                {
                    if (i >= tileSheet.Count)
                    {
                        break;
                    }

                    var offset = Vector3.zero;
                    if (item.Value.hasOffset)
                    {
                        offset = item.Value.positionOffset - tilemap.tileAnchor;

                        var cellSize = tilemap.cellSize;
                        if (wasEmpty)
                        {
                            cellSize = item.Value.scaleFactor;
                        }
                        offset.x *= cellSize.x;
                        offset.y *= cellSize.y;
                        offset.z *= cellSize.z;
                    }

                    SetTile(tilemap
                            , targetPosition + item.Key
                            , tileSheet[i++]
                            , Color.white
                            , Matrix4x4.TRS(offset, Quaternion.identity, Vector3.one));
                }
                OnPaletteChanged();

                m_PaletteNeedsSave = true;
                FlushHoverData();
                GUI.changed = true;
                SavePaletteIfNecessary();

                if (wasEmpty)
                {
                    ResetPreviewInstance();
                    FrameEntirePalette();
                }

                Event.current.Use();
                GUIUtility.ExitGUI();
            }
            break;

            case EventType.Repaint:
                // Handled in Render()
                break;
            }

            if (m_HoverData != null && (
                    Event.current.type == EventType.DragExited ||
                    Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Escape))
            {
                DragAndDrop.visualMode = DragAndDropVisualMode.None;
                FlushHoverData();
                Event.current.Use();
            }
        }