Table of Contents

Class DraggableItem<T>

Abstract MonoBehaviour base class for draggable UI items in the Drag-and-Drop system. Handles pointer events, canvas hierarchy management (render-on-top), transparency during drag, and optional snap-back to the original position.

public abstract class DraggableItem<T> : MonoBehaviour, IDraggable<T>, IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler where T : class

Type Parameters

T

The payload type associated with this draggable item. Must be a reference type.

Inheritance
DraggableItem<T>
Implements
IBeginDragHandler
IDragHandler
IEndDragHandler
IDropHandler

Remarks

Requires a Canvas with a GraphicRaycaster on the UI canvas, a CanvasGroup component for the transparency effect, and a reference to the root dragCanvas for sibling-order management.

Fields

OnDragEnd

Invoked when a drag operation ends, passing the item's current data.

public UnityAction<T> OnDragEnd

Field Value

UnityAction<T>

OnDragStart

Invoked when a drag operation starts, passing the item's current data.

public UnityAction<T> OnDragStart

Field Value

UnityAction<T>

OnDragging

Invoked every frame during a drag, passing the data and current world position.

public UnityAction<T, Vector3> OnDragging

Field Value

UnityAction<T, Vector3>

allowSwapping

protected bool allowSwapping

Field Value

bool

canvasGroup

protected CanvasGroup canvasGroup

Field Value

CanvasGroup

data

protected T data

Field Value

T

dragAlpha

protected float dragAlpha

Field Value

float

dragCanvas

protected Canvas dragCanvas

Field Value

Canvas

isDragging

protected bool isDragging

Field Value

bool

originalParent

protected Transform originalParent

Field Value

Transform

originalPosition

protected Vector3 originalPosition

Field Value

Vector3

originalSiblingIndex

protected int originalSiblingIndex

Field Value

int

returnToOriginalPosition

protected bool returnToOriginalPosition

Field Value

bool

wasDropped

protected bool wasDropped

Field Value

bool

Methods

CanReturnToOriginalPosition()

Returns true if this item should snap back to its original position after an unsuccessful drop.

public abstract bool CanReturnToOriginalPosition()

Returns

bool

CanStartDrag()

protected virtual bool CanStartDrag()

Returns

bool

CanSwapWith(IDraggable<T>)

Returns true if this item is allowed to swap data with other.

public abstract bool CanSwapWith(IDraggable<T> other)

Parameters

other IDraggable<T>

Returns

bool

GetData()

Returns the data payload carried by this draggable item.

public virtual T GetData()

Returns

T

GetTransform()

Returns the Transform of the draggable GameObject.

public virtual Transform GetTransform()

Returns

Transform

OnBeginDrag(PointerEventData)

public virtual void OnBeginDrag(PointerEventData eventData)

Parameters

eventData PointerEventData

OnDrag(PointerEventData)

public virtual void OnDrag(PointerEventData eventData)

Parameters

eventData PointerEventData

OnDragEnded(bool)

Called by the drag system when a drag operation ends.

public virtual void OnDragEnded(bool wasDropped)

Parameters

wasDropped bool

true if the item was dropped onto a valid DropZone<T>.

OnDragStarted()

Called by the drag system when a drag operation begins.

public virtual void OnDragStarted()

OnDrop(PointerEventData)

public virtual void OnDrop(PointerEventData eventData)

Parameters

eventData PointerEventData

OnEndDrag(PointerEventData)

public virtual void OnEndDrag(PointerEventData eventData)

Parameters

eventData PointerEventData

ReturnToOriginalPosition()

protected virtual void ReturnToOriginalPosition()

SetData(T)

Assigns a new data payload to this draggable item.

public virtual void SetData(T data)

Parameters

data T

SwapWith(IDraggable<T>)

Performs the data swap between this item and other.

public abstract void SwapWith(IDraggable<T> other)

Parameters

other IDraggable<T>