Ordering of UI elements with layers instead of hierarchy position

( Hopefully i am posting to correct forum, i am not sure if feature request for Unity UI belongs here )

I find the current system for ordering images and texts with position in the hierarchy cumbersome. It would be great if we could get something which was working very well long time ago in NGUI and that was ordering of elements by index property. Ie you could just order element visibility with a number.

The current structure makes it very difficult to manage proper layering with UI created in runtime ( things like inventory, items in inventory, tooltips in the inventory etc ). Creating all these things from script adds additional complexity of parenting everything correctly and then when for example item from inventory is dragged it has to be moved from within the inventory slot, moved in hierarchy on top of all other slots, so its actually visible on top of other items and then attached to new slot.
The whole management of hierarchy could be completed removed with indexed layers for each item, so that only changing the index in the code would allow to display element on top of others. Well, it would certainly make things easier for me. I am not sure how others feel about this.

Create gameobject layers and name accordingly. Just move children about the GOs so there is no managing index numbers. And the hierarchy documents itself. Create companion routines to remove and inject inventories into associated GOs. You either write GO code or you write index code. There is no escape. Using GOs then keeps Unity changes off the table. Or write your own asset and sell it!

Perhaps you missed my point? I know how to work with the system, all i am saying is that layer index would make things much easier. I would hope that your own explanation how to work around the system would make it obvious.

Instead of re-parenting, you can try adding Canvas component and overriding Sort Order. If you need events, you have to add Graphic Raycaster too. I use this when I want different sorting within Vertical Layout

3 Likes

Interesting. is it good idea to have many canvas components? Like for example in the case of inventory and all the items?

Basically, nested canvases, in some cases, even boost your UI performance (for example, if you split your static and dynamic UI elements into 2 nested canvases)

I am not sure about your example, because if I got it right, you’d want to add nested canvas for each inventory item? That may cripple your performance after all, but you can always check profiler and see how much. But instead of adding nested canvases in inspector, you can instantiate those components in your script while dragging begin and then destroy them on drop. It would be maybe easier than reparenting

1 Like