- Home /
Managing GUI.depth for drawing lines in EditorWindow
Hello,
I am currently doing a state-machine-like editor like The Spaghetti Machine or PlayMaker. Basically, I would like to display boxes and connect them together to represent a work-flow. In addition I would like to be able to edit interactively the content during play-mode.
I've started my work with EditorWindow where I display draggable windows (using GUI.DragWindow()) connected throw this code to draw Bezier curves.
My problem is related with the depth of the Bezier curves. If you take a look on the pictures for drawing lines, you will see that the Bezier curves remain behind the draggable windows, not in front.
I looked for solutions to cope with depth and found that I should use per-script settings for GUI.depth. But I do not see how I can achieve a drawing system like: one script draws the boxes, another one draws the Bezier curves. I thought that both the scripts could inherit from EditorWindow but I then don't know how to instantiate the script that is not launched by the Unity menu. I also thought that I could make the drawing-lines script inherit from MonoBehavior and tag it as ExecuteInEditMode but its OnGUI method is only called when the user interacted with the GUI. So, how can I manage the depth?! The Spaghetti Machine did an awesome work...
Answer by Zogg · Feb 18, 2012 at 03:23 AM
It's actually simpler than you thought: In OnGUI, what's drawn first is behind what's drawn second, except for windows drawn inside the BeginWindows-EndWindows block.
In the Spaghetti Machine, the lines are displayed behind the panels because they are drawn first. Boiled down to what is relevant to your question, the OnGUI method looks like this:
public void OnGUI ()
{
// Display links - they appear behind the panels
foreach( EditorLink link in maLinks )
{
link.DrawLink();
}
// Display panels
BeginWindows();
for( int i = 0; i < maPanels.Length; i++ )
{
EditorPanel panel = maPanels[i] as EditorPanel;
panel.mWindowRect = GUI.Window( iID, panel.mWindowRect, DoPanelWindow, strTitle );
}
EndWindows();
}
I just made a quick test what happens when we draw the lines after EndWindows() :
public void OnGUI ()
{
// Display panels
BeginWindows();
for( int i = 0; i < maPanels.Length; i++ )
{
EditorPanel panel = maPanels[i] as EditorPanel;
panel.mWindowRect = GUI.Window( iID, panel.mWindowRect, DoPanelWindow, strTitle );
}
EndWindows();
// Display links - they appear in front of the panels
foreach( EditorLink link in maLinks )
{
link.DrawLink();
}
}
In this case, the lines are actually drawn in front of the panels.
Your answer
![](https://koobas.hobune.stream/wayback/20220613063827im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using Playmaker for a game-scope FSM 0 Answers
[Custom Editor Windows] Recreating Animator Controller Like Features 0 Answers
How do I stop new elements in a list/array from inheriting values from the last element? 1 Answer
Editor GUI Foldout header style customization 0 Answers
Scene Window goes grey in some scenes? 0 Answers