- Home /
How to change UI rendering order without changing hierarchy
I'm creating Inventory system. When i drag object around if it is not the last sibling it will be rendered under the other slots in the inventory. Because I'm using Grind Layout to place the slots around i cant change the slot order in hierarchy (it will move it to be last slot). I tried adding a canvas component and check override sorting but then i cant interact with it move it around and click on buttons. is there a way of changing rendering UI order without changing hierarchy?
Answer by HappiiGamer · Oct 15, 2019 at 01:25 PM
For UI the Canvas Component is the only way. Your solution does work it's just that you need to add a GraphicRaycast component to your transform in order to interact with it.
Answer by frankyfrankfrank · Sep 29, 2020 at 04:07 PM
Hey @uri2563, I just had to figure this out as well.
To do this you need to:
Add an Event Trigger component to your prefab (or just your GameObject) that you want to be set as the last sibling
Click Add New Event Type and choose Pointer Down
Click the + (Plus) button in the bottom right corner of your new event type to add a new event handler
Drag the prefab into the slot where it says None (Object)
In the dropdown that now says 'No Function', find and mouse over RectTransform and then SetAsLastSibling()
Now, the game object is listening for the PointerDown event and when that is triggered, it will find the RectTransform you set, and fire the SetAsLastSibling method.
Now that item is in front of all the others!
Try playing with the other event triggers. You may find something that works even better than this!
Your answer
Follow this Question
Related Questions
Canvas items turned to be blank images 0 Answers
Unity Check overlap of rect works except if in gridlayoutgroup 0 Answers
How to create a "back" side of a canvas UI element 1 Answer
Applying full screen image effects to UI only 2 Answers
Interface sprite rendering artifact—sprites outlined by thin black line 0 Answers