Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Drag UI Elements Between Panels/SiblingIndex

Discussion in 'Unity UI & 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.