Class DraggableItem<T>
- Namespace
- WitShells.DesignPatterns.Core
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
TThe payload type associated with this draggable item. Must be a reference type.
- Inheritance
-
DraggableItem<T>
- Implements
-
IDraggable<T>IBeginDragHandlerIDragHandlerIEndDragHandlerIDropHandler
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
canvasGroup
protected CanvasGroup canvasGroup
Field Value
- CanvasGroup
data
protected T data
Field Value
- T
dragAlpha
protected float dragAlpha
Field Value
dragCanvas
protected Canvas dragCanvas
Field Value
- Canvas
isDragging
protected bool isDragging
Field Value
originalParent
protected Transform originalParent
Field Value
- Transform
originalPosition
protected Vector3 originalPosition
Field Value
- Vector3
originalSiblingIndex
protected int originalSiblingIndex
Field Value
returnToOriginalPosition
protected bool returnToOriginalPosition
Field Value
wasDropped
protected bool wasDropped
Field Value
Methods
CanReturnToOriginalPosition()
Returns true if this item should snap back to its original position after an unsuccessful drop.
public abstract bool CanReturnToOriginalPosition()
Returns
CanStartDrag()
protected virtual bool CanStartDrag()
Returns
CanSwapWith(IDraggable<T>)
Returns true if this item is allowed to swap data with other.
public abstract bool CanSwapWith(IDraggable<T> other)
Parameters
otherIDraggable<T>
Returns
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
eventDataPointerEventData
OnDrag(PointerEventData)
public virtual void OnDrag(PointerEventData eventData)
Parameters
eventDataPointerEventData
OnDragEnded(bool)
Called by the drag system when a drag operation ends.
public virtual void OnDragEnded(bool wasDropped)
Parameters
wasDroppedbooltrueif 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
eventDataPointerEventData
OnEndDrag(PointerEventData)
public virtual void OnEndDrag(PointerEventData eventData)
Parameters
eventDataPointerEventData
ReturnToOriginalPosition()
protected virtual void ReturnToOriginalPosition()
SetData(T)
Assigns a new data payload to this draggable item.
public virtual void SetData(T data)
Parameters
dataT
SwapWith(IDraggable<T>)
Performs the data swap between this item and other.
public abstract void SwapWith(IDraggable<T> other)
Parameters
otherIDraggable<T>