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: what the grid layout group looks like 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: problem 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

+3


source to share


1 answer


Try adding a "Canvas" component to your collection and check "override sort" and set the sort order. I think I also had to add a graphics raycaster.

Inspector:



inspector

0


source







All Articles