- Home /
GUI.button order
I have this couples of repeatbuttons in one of my script. Whenever I click a button, the button will be able to drag.
My problem is whenever I dragged the button hovering other button, it's rendered behind the other buttons.
Here's how I render them
Code:
function OnGUI()
{
for(var i = 0;i < transform.childCount; i++)
{
TogglePanel[i] = GUI.RepeatButton (itemRect[i], "",
ItemPanel);
if(TogglePanel[i])
{
if(!dragging)
{
dragging = true;
originPos = itemRect[i];
}
if(dragging)
Dragging(i, originPos);
}
}
}
How do I set it so it's rendered on the top?
I need some help for this...thanks ^^
Answer by fredpointzero · Aug 25, 2011 at 08:00 AM
There are two solutions based on where you draw your GUI.
On the right hand, you draw your GUI in one script, so the depth order is defined by the order of the call of GUI / GUILayout functions. To have a gui element draw over others, you should draw it at the end of your OnGUI method.
On the other hand, you draw your GUI with several scripts. You should consider using GUI.depth to order your gui through layers.
That a good solution of you :D
I don't want to do it for several scripts for now....Since it's been a hard work doing it in a script..hahahaha LOL
is there any idea how to draw it at the end? since all the buttons is not rendered separately but as a group...Do I need to rerender it at the end?
dragging GUI elements can have some ugly sideeffects. $$anonymous$$eep in $$anonymous$$d that when you have overlapping buttons only the first one will react to the user input but it is drawn at the bottom. The last button is always on top of all others because unity draws the button in the order you call GUI.RepeatButton.
If you really want draggable GUI elements you should use a GUI.Window for each element. In a Window you can use GUI.DragWindow to drag the window ;)
windows can change their z-ordering automatically since Unity stores all GUI.Window calls internally and draw the windows right after OnGUI. Also the input is handled correctly when two or more windows are overlapping.
@Bunny83 is right, you can use the native window system that support drag and drop, it will save you some coding time !
Your answer
![](https://koobas.hobune.stream/wayback/20220612191636im_/https://answers.unity.com/themes/thub/images/avi.jpg)