Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UI Draw Order when dragging

Discussion in 'UGUI & TextMesh Pro' started by egellon, Sep 18, 2015.

  1. egellon

    egellon

    Joined:
    May 19, 2014
    Posts:
    2
    Hello.

    Recently I've encountered an issue which I couldn't find a solution for. The situation is as described:
    The game is 2D. In my UI layer (in a Canvas object) there are three objects: "LeftToken", "MiddleToken", "RightToken" - each of these objects is an UI Image. All of them has a child 'UITokenRenderer' which is basically also an Image. The UITokenRenderer is draggable and everything seems to be fine until I drag the UIToken. Then certain elements of UI cover the dragged object (which is understanadble, because of the fact that the draw order in UnityUI is based on elements position in hierarchy). But the question is, how to make currently dragged UITokenRenderer always on top ?

    I was testing with different settings of render mode in Canvas trying to modify the 'Z' position of certain elements but that didn't help :(
     
  2. BinaryX

    BinaryX

    Joined:
    Aug 4, 2014
    Posts:
    55
    You kinda responded to the question yourself, but you didn't go in the right direction with your research.

    Indeed, that's what's happening there. RectTransform has several methods to help you with that.
    When you start the dragging action, save the index at which the object is currently placed at with "GetSiblingIndex()" and then use "SetAsLastSibling()" to put it at the bottom of the siblings (on top of every other image). After the dragging is over, you can set the index back with "SetSiblingIndex()".

    I hope it helped. Have a great day.
     
    Misnomer and SimonDarksideJ like this.
  3. egellon

    egellon

    Joined:
    May 19, 2014
    Posts:
    2
    Yes, that helped :) Thank you very much!