Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. 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 at 3:49 AM.

  1. daileyml

    daileyml

    Joined:
    Aug 18, 2017
    Posts:
    1
    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.     }
    7.  
    8.     public void OnEndDrag(PointerEventData eventData)
    9.     {
    10.         transform.localPosition = Vector3.zero;
    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

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    344
    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.