Class DragAndDrop
The DragAndDrop class provides a way to manage drag and drop operations in App UI.
Inherited Members
Namespace: Unity.AppUI.Core
Assembly: Unity.AppUI.dll
Syntax
public static class DragAndDrop
Remarks
You can use this class to handle drag and drop at Runtime or in the Editor.
Examples
using Unity.AppUI.Core;
// be sure to call this method during a PointerDown or PointerMove event to work properly.
public void StartDraggingItems()
{
var draggedItems = GetItemsToDrag();
DragAndDrop.PrepareStartDrag();
DragAndDrop.objects = draggedItems;
DragAndDrop.StartDrag($"{draggedItems.Count} list item{(draggedItems.Count > 1 ? "s" : "")}");
}
If you want to handle dropping items, you can use the DropZone UI element, or the DropZoneController controller for a lower-level approach.
using Unity.AppUI.Core;
using Unity.AppUI.UI;
public class MyMainView : VisualElement
{
public MyMainView()
{
var dropZone = new DropZone();
dropZone.controller.acceptDrag = ShouldAcceptDrag;
dropZone.controller.dropped += OnDropped;
dropZone.controller.dragEnded += CleanUp;
Add(dropZone);
}
bool ShouldAcceptDrag(IEnumerable<object> draggedObjects)
{
// return true or false depending on if the drop zone should accept the dragged objects
}
void OnDropped(IEnumerable<object> droppedItems)
{
// handle the dropped items here...
CleanUp();
}
void CleanUp()
{
// clean up any state set up when the drag operation started
}
}
Properties
activeDropTarget
The active drop target.
Declaration
public static Manipulator activeDropTarget { get; }
Property Value
Type | Description |
---|---|
Manipulator |
count
The number of objects being dragged.
Declaration
public static int count { get; }
Property Value
Type | Description |
---|---|
int |
objects
The objects being dragged.
Declaration
public static IEnumerable<object> objects { get; set; }
Property Value
Type | Description |
---|---|
IEnumerable<object> |
state
The current state of the drag and drop operation.
Declaration
public static DragAndDropState state { get; set; }
Property Value
Type | Description |
---|---|
DragAndDropState |
Methods
Drop()
Drops the dragged objects.
Declaration
public static IEnumerable<object> Drop()
Returns
Type | Description |
---|---|
IEnumerable<object> | The dropped objects. |
Remarks
This is the equivalent of AcceptDrag.
PrepareStartDrag()
Clears drag and drop data.
Declaration
public static void PrepareStartDrag()
StartDrag(string)
Start a drag operation.
Declaration
public static void StartDrag(string title)
Parameters
Type | Name | Description |
---|---|---|
string | title | The title of the drag operation. |
Remarks
This method must be called after setting the dragged objects, and during a PointerDown or PointerMove event.