Drag into unity, drag over other UI elements
I have a panel with a component Grid Layout Group
. Inside I got a panel called background image slot, and inside that I got an assembly of two images. it looks like this:
I wrote a drag and drop script found in TankPrefab
:
public class DragHandler : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler {
public static GameObject itemBeingDragged;
Vector3 startPosition;
public void OnBeginDrag(PointerEventData eventData)
{
itemBeingDragged = gameObject;
startPosition = transform.position;
}
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
itemBeingDragged = null;
transform.position = startPosition;
}
}
I have a problem when I drag the tank over the slot found after the slot. it picks up the tank behind it:
I tried transform.SetAsLastSibling();
in OnBeginDrag
, but only move it to the first place in the parent (slot) and I want it to be above the other slots, so I also tried transform.parent.SetAsLastSibling();
it and it looks like this at first, but after 2 drags tank disappears
source to share