Created
July 22, 2024 04:02
-
-
Save SolarianZ/6f5ae2fa7d33e7a2048b4c6f4c32dc8c to your computer and use it in GitHub Desktop.
{"category": "Unity Engine/Editor/Extensions", "keywords": "Unity, Editor, UIToolkit, UIEElement, DragAndDrop, Manipulator"} Manipulator to simplify the implementation of VisualElement drag-and-drop functionality.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Collections.Generic; | |
| using UnityEditor; | |
| using UnityEngine; | |
| using UnityEngine.UIElements; | |
| public interface IDragAndDropDataProvider | |
| { | |
| /// <summary> | |
| /// Get data associated with current drag and drop operation. | |
| /// </summary> | |
| /// <returns></returns> | |
| IEnumerable<KeyValuePair<string, object>> GetGenericData(); | |
| /// <summary> | |
| /// Get the objects being dragged. | |
| /// </summary> | |
| /// <returns></returns> | |
| Object[] GetObjectReferences(); | |
| /// <summary> | |
| /// Get the file names being dragged. | |
| /// </summary> | |
| /// <returns></returns> | |
| string[] GetPaths(); | |
| /// <summary> | |
| /// Get the dragging title. | |
| /// </summary> | |
| /// <returns></returns> | |
| string GetTitle(); | |
| } | |
| public class PointerDragAndDropManipulator : PointerManipulator | |
| { | |
| private readonly IDragAndDropDataProvider _dragAndDropDataProvider; | |
| private bool _draggable; | |
| public PointerDragAndDropManipulator(IDragAndDropDataProvider dragAndDropDataProvider) | |
| { | |
| _dragAndDropDataProvider = dragAndDropDataProvider; | |
| } | |
| protected override void RegisterCallbacksOnTarget() | |
| { | |
| target.RegisterCallback<PointerDownEvent>(OnPointerDown); | |
| target.RegisterCallback<PointerUpEvent>(OnPointerUp); | |
| target.RegisterCallback<PointerMoveEvent>(OnPointerMove); | |
| } | |
| protected override void UnregisterCallbacksFromTarget() | |
| { | |
| target.UnregisterCallback<PointerDownEvent>(OnPointerDown); | |
| target.UnregisterCallback<PointerUpEvent>(OnPointerUp); | |
| target.UnregisterCallback<PointerMoveEvent>(OnPointerMove); | |
| } | |
| private void OnPointerDown(PointerDownEvent evt) | |
| { | |
| evt.StopImmediatePropagation(); | |
| _draggable = true; | |
| } | |
| private void OnPointerUp(PointerUpEvent evt) | |
| { | |
| if (_draggable) | |
| { | |
| evt.StopImmediatePropagation(); | |
| _draggable = false; | |
| } | |
| } | |
| private void OnPointerMove(PointerMoveEvent evt) | |
| { | |
| if (_draggable) | |
| { | |
| evt.StopImmediatePropagation(); | |
| _draggable = false; | |
| DragAndDrop.PrepareStartDrag(); | |
| IEnumerable<KeyValuePair<string, object>> genericData = _dragAndDropDataProvider.GetGenericData(); | |
| if (genericData != null) | |
| { | |
| foreach (KeyValuePair<string, object> kv in genericData) | |
| { | |
| DragAndDrop.SetGenericData(kv.Key, kv.Value); | |
| } | |
| } | |
| DragAndDrop.objectReferences = _dragAndDropDataProvider.GetObjectReferences(); | |
| DragAndDrop.paths = _dragAndDropDataProvider.GetPaths(); | |
| DragAndDrop.StartDrag(_dragAndDropDataProvider.GetTitle()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment