- Home /
Making a collapsible menu in the inspector
I noticed some scripts have different options in collapsible menus in the inspector. For example there's a collapsible menu named "Resolution" and when you open it, it shows the properties.
How do they do it?
In c# preferred, but I guess I could possibly convert from js. Thanks
Answer by Molix · Mar 01, 2011 at 05:26 PM
You can use the EditorGUILayout.Foldout control. It is basically just a special toggle control; assign it to a boolean, and if it is true then draw the controls "inside", if it is false then don't.
That requires using unityeditor, I don't want that. I've seen this being done without unityeditor
Your question does say "in the inspector". Anyway, there isn't really any magic to it; it is just a toggle that displays a little arrow icon pointing sideways or down. So if you want one at runtime, just use Toggle with a couple custom styles that contain arrows.
Answer by znoey · Aug 13, 2012 at 04:45 PM
I'm sure this question is incredibly old but i stumbled upon it while looking for something else.
Based on the comments you need to make a serializable class to contain the data you want to be foldable. So in my FakeDataBehaviour as a monobehaviour, we have a FakeData class that contains something interesting. We define multiple ones inside my class. In the inspector, these will appear as foldable objects for setting the specific values.
for example:
public class FakeDataBehaviour : MonoBehaviour
{ [System.Serializable] public class Data { string name; float someValue; }
[SerializeField] Data myData1; [SerializeField] Data myData2; // do more stuff... }
Answer by EuanHollidge · Jun 30, 2017 at 01:57 PM
I know this is years ago but I think the answer you were looking for was to create a Struct with the vars inside then create a value for it. That gives the fold up menu with the GUI stuff.
Answer by normand · Oct 01, 2018 at 02:19 AM
private int p_workingMode = 0;
public override void OnInspectorGUI(){
GUIContent[] operationsOptions = new GUIContent[ 3]; //////Number of button in the ToolBar
operationsOptions[ 0 ] = new GUIContent( "Your Menu Text1", your Icon1 );
operationsOptions[ 1 ] = new GUIContent( "Your Menu Text2", your Icon2 );
operationsOptions[2 ] = new GUIContent( "Your Menu Text3", your Icon3 );
p_workingMode = GUILayout.Toolbar( p_workingMode, operationsOptions );
EditorGUILayout.Separator();
switch (p_workingMode) {
case 0:
TerrainTool();
break;
case 1:
TextureTool();
break;
case 2:
VegetationTool();
break;
}
}
void TerrainTool(){
////your stuff too show
EditorGUILayout.PropertyField( your variable);
if (GUILayout.Button( "Your name of the button" )) {
your void();
}
}
Your answer
