- Home /
Can I implement Sorting Layers behavior in new UI?
Hi,
I have an inventory and it has two major groups in UI: Weapons and Potions. They both have multiple child objects.
In hierarchy weapons stay below potions. It means that its children will always appear above potions children.
I'm implementing drag&drop. So when I drag a potion - i'm setting it as last sibling and it will appear above all other potions. But when I hover over weapons - it will render under them because weapons parent is below in hierarchy.
Is there a way to directly tell dragged potion to render above all other UI elements or I need to fiddle with parents - change they order in hierarchy every time an item is dragged?
Answer by TonicMind · Dec 17, 2015 at 05:05 PM
You should keep a reference to the layer of the hierarch that you want a potion (or weapon) to be. This may require some minor but manageable changes to your hierarchy.
So: 1. In your hierarch make sure you have two empty gameobjects for the layers. It should look like this:
1. In your script either create a public variable to assign the layer to from the unity editor inspector, or find the layered object like so: PotionLayerReference = GameObject.Find("PotionLayer"); 2. When you want to place your weapon or potion object on the specific layer, simply attach the desired object to that referenced layer. That should put it on the just below the layer object.
Like so: HealthPotionTwo.transform.setParent(PotionLayerReference);
Answer by nTu4Ka · Jan 11, 2016 at 01:48 PM
This is so stupid. I managed to solve problem with weapons and potions but now have to solve problem with other UI elements since:
-if I put inventory at the bottom of hierarchy - its elements will overlap other UI elements
-if I put higher - items won't appear correctly when dragging
Now I feel that I need to write some kind of hierarchy manager just to make UI appear correctly.
You could create a top layer to put the items in temporarily while being dragged.
This actually is a good in it's simplicity solution. But I would have to set up a manager of some sort to have one instrument that manipulates hierarchy in order not to mess up the tree because after drag is over you need to place manipulated parent where he was. In other case ordering would be slightly messed up and can affect other part of game.
After a little research (google :P) I found a solution for you!
There's an option within each canvas called Sorting, you can define new sorting layers with it. All you need to do is create a new Canvas for each object you want to be rendered in whatever order you wish. You'll set the layer in the Canvas component within the Inspector Canvas panel. I'd post a picture but my Uni's internet sucks right now, won't let me upload anything, oddly. I'm connected via ethernet, normally its the Wifi, though I've known it to go down.
So basically, just create Canvas Objects with the same render mode (ScreenSpace Layout or something, whichever is default for your game) and set their Sorting layers. You should attach them all beneath one primary canvas. $$anonymous$$ake sure you check the transform values too, something odd happened with the scaling and size when I put together my example.
Wow. This is interesting. And looks like requires less coding then creating a hierarchy manager.