Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Drag UI Elements Between Panels/SiblingIndex

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by daileyml, Aug 12, 2018.

  1. daileyml


    Aug 18, 2017
    Hi. I'm working on the UI portion of a 3D game, and trying to find the solution to dragging UI elements from one UI panel to another (essentially dragging "items" from game "inventory" to the "action bar"). These would be two different panels on the same canvas, and so far the only solution I've managed to find looks similar to this:

    Code (CSharp):
    1.     public void OnDrag(PointerEventData eventData)
    2.     {
    3.         transform.parent.parent.parent.SetAsLastSibling();
    4.         transform.parent.SetAsLastSibling();
    5.         transform.position = Input.mousePosition;
    6.     }
    8.     public void OnEndDrag(PointerEventData eventData)
    9.     {
    10.         transform.localPosition =;
    11.     }
    As you can see, using "parent.parent.parent" is less than ideal but without it, when the icons are dragged between panels they move behind the images on the destination panel. I understand this involves the hierarchy and index of the elements, but I'm struggling to find a good solution to dynamically adjust the index as elements are dragged.

    Anyone have a good reference or example they could point me to? Most examples are changing index order between child elements under the same parent. This obviously doesn't apply here.

    Thanks in advance for your help.
  2. Hosnkobf


    Aug 23, 2016
    I would create an empty game object in the canvas (in the hierarchy at the bottom, so that it is rendered on top) which covers the whole screen.
    Reference that object from your drag-component and move dragged elements there for as long as they are dragged.