- Home /
UI Panel BringToFront
In the old GUI system for unity, there was GUI.BringToFront. Is there a way to do the equivalent (telling the UI drawing system to draw a particular element in front of all others) for UI elements?
Googling around for this, I am only finding references to a the old GUI system.
I am building an inventory system that has tooltips, where my tooltip is a UI Panel with some images/text. I can draw the panel when I want, but it is hidden by other UI elements that are below it in my project hierarchy.
Answer by TonyGon · Jul 03, 2016 at 12:41 PM
Programmatically you can achieve this by getting the RectTransform
of your element and setting it as last sibling in the hierarchy, i.e.
RectTransform theRectTransform;
...
theRectTransform = transform as RectTransform; // Cast it to RectTransform
panelRectTrafo.SetAsLastSibling(); // Make the panel show on top.
This works since the elements are rendered in the order they appear in the hierarchy, and setting the SetAsLastSibling()
method is equivalent to dragging the element in the editor below other elements, which in turn makes it render on top.
Answer by alok-kr-029 · Dec 28, 2014 at 01:58 PM
The UI element you want to keep above inside the panel just drag and drop that element at bottom of the hierarchy inside the panel all the ui element must be child of the panel
Each of my inventory slots have a tooltip panel as children. I instantiate each of the slots at runtime and add them to a 'background' container panel, resulting in a hierarchy that has a bunch of slots at the same hierarchical level each with a child tooltip. This means that for each slot that is not the last instantiated slot, there are slots below it on the heirarchy, and their panels are diplayed in front of the tooltip panel for the slot above it.
Perhaps a better solution for me would be to have a tooltip prefab placed at the bottom of my hierarchy and simply instantiate it and position it in 2D space over the icon I mouse over, rather than each slot having their own tooltip.
This said, I can imagine situations where it would be desirable to be able to set the rendering layer at runtime, and I'm confident there is a way to do this - just haven't been able to find out how. It seems with a newly designed UI system, something analogous to 'bring this UI element to the front of all the others' would be a natural thing to include in the UI API.
What I've done is created my tooltip panel at the bottom of the Canvas hierarchy (so that it renders on top of all other UI elements), set it to inactive (since it isn't suppose to be displayed by default), dragged it into a GameObject variable in my Inventory script, then when I instantiate the individual inventoryslot objects from within my Inventory script, I provide a reference to the Tooltip panel to each slot.
I am still interested in learning about a way to programatically bring a UI element to the front of the screen. It certainly seems like this would be a normal, common thing to do.
Answer by aal-ok · Jul 03, 2016 at 02:01 PM
or, you can just change the layer of the image to bring it to front, 1 for front and so on
Your answer
Follow this Question
Related Questions
Combining Mesh changes Draw Order? 3 Answers
How to draw in specific panel? 0 Answers
Drawing a 3D object javascipt 0 Answers
to draw a health bar 2 Answers
How to draw an outline around multiple hexagon game objects? 1 Answer