Who create a group on a custom Editor like a class group?

When you creates a class on a script, the variables appears on the editor inside a group that can be maximized an minimized. How can I create this on a custom editor?

Like this, for example:

38512-screenshot_1.png

It’s just a foldout, and indented fields. Something like this should work:

foldout = EditorGUILayout.Foldout(foldout, "Network whatever");
if (foldout) 
{
   var level = EditorGUI.indentLevel;
   EditorGUI.indentLevel++;
   // ... whatever fields you want
   EditorGUI.indentLevel = level;
}

‘foldout’ is a boolean you define somewhere in your editor script


EDIT (BONUS): Since we mentioned indentation, if you write a lot of editor codes, you might want to make a GUI library that makes things a bit more convenient/easy to use/faster to type. For example, it is very common to increase indentation and then revert it back, same thing for GUI.color and EditorGUILayout.labelWidth, GUI.changed, and some others. Another thing is the “blocks” so you have a BeginHorizontal and a corresponding EndHorizontal. One might find that a lot to type, and you also have to remember to insert an Ending. So can we do something to limit mistakes and reduce typing?

Sure, ‘using’ and IDisposable are your friends!

public class GUIColorBlock : IDisposable
{
	private Color prevColor;

	public GUIColorBlock Begin(Color newColor)
	{
		prevColor = GUI.color;
		GUI.color = newColor;
		return this;
	}

	public void Dispose()
	{
		GUI.color = prevColor;
	}
}

public static class gLib // for GUILibrary
{
    private static GUIColorBlock colorBlock = new GUIColorBlock();

    public static GUIColorBlock ColorBlock(Color c)
    {
       return colorBlock.Begin(c);
    }
}

Then in your editor scripts, instead of doing:

var old = GUI.color;
GUI.color = newColor;
// some gui controls
GUI.color = old;

you can now say:

using (gLib.ColorBlock(newColor))
{
   // your gui controls!
}

When the scope of the using statement ends, ‘Dispose’ gets called and the colors gets back to the previous value! - obviously you can follow similar patterns to indentation, begin/end blocks etc.

(lookup ‘using’ and IDisposable if you’re unfamiliar with them)

And if you like writing GUI code like, check out RabbitGUI, my custom layout system in VFW :slight_smile:

Hope that helps!