UI Draw Order when dragging

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 :frowning:

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.

2 Likes

Yes, that helped :slight_smile: Thank you very much!