- Home /
Question by
RIw · Feb 23, 2016 at 08:35 AM ·
c#uidrag-and-drop
Unity UI Drag and Drop - Dragged items are being hovered by their Dropzones
Hello. I'm actually learning the new Unity UI System, and I want to make a simple Dragging in the Grid Layout Group. Dragging is working fine, but Item is under the slot while dragging.
//DragHandler.cs
public void OnBeginDrag(PointerEventData eventData)
{
startParent = transform.parent;
transform.SetParent(slotPanel.transform.parent);
transform.SetSiblingIndex(1);
}
public void OnDrag(PointerEventData eventData)
{
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
if (transform.parent == slotPanel.transform.parent)
{
transform.SetParent(startParent);
transform.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
}
else if(transform.GetComponent<RectTransform>().anchoredPosition != Vector2.zero)
{
transform.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
}
}
//DropZone.cs
public void OnDrop(PointerEventData eventData)
{
eventData.pointerDrag.transform.SetParent(transform);
eventData.pointerDrag.transform.GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
}
I have been trying to set the SiblingIndex in the OnBeginDrag method to bigger than 1. They the draggedItem is displaying well, but OnDrop method doesn't call
Comment
Answer by MehYam · Feb 19, 2017 at 02:30 AM
Just try setting the parent during the drag to something further up in the hierarchy; it should probably be set to the top-level canvas. Worked for me, without any SetSiblingIndex fudging.
Your answer
![](https://koobas.hobune.stream/wayback/20220612065302im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# UI list item drag onto 2d sprite in world space? 1 Answer
Which layout and how? 0 Answers