EditorGUILayout.Foldout 是 Unity 编辑器扩展中用于创建可折叠区域的控件。它允许你在自定义 Editor 或 EditorWindow 中组织大量选项或设置,使得界面更加整洁和易于导航。
using UnityEditor;
using UnityEngine;
public class FoldoutExample : EditorWindow
{
private bool showFoldout1 = true;
private bool showFoldout2 = false;
[MenuItem("Window/Foldout Example")]
public static void ShowWindow()
{
GetWindow<FoldoutExample>("Foldout Example");
}
private void OnGUI()
{
GUILayout.Label("Foldout Example", EditorStyles.boldLabel);
// First foldout section
showFoldout1 = EditorGUILayout.Foldout(showFoldout1, "Settings Section 1");
if (showFoldout1)
{
EditorGUILayout.BeginVertical(GUI.skin.box);
EditorGUILayout.LabelField("Setting 1", EditorStyles.label);
EditorGUILayout.FloatField("Float Value", 1.23f);
EditorGUILayout.Toggle("Toggle Option", true);
EditorGUILayout.EndVertical();
}
// Second foldout section
showFoldout2 = EditorGUILayout.Foldout(showFoldout2, "Settings Section 2");
if (showFoldout2)
{
EditorGUILayout.BeginVertical(GUI.skin.box);
EditorGUILayout.LabelField("Setting 2", EditorStyles.label);
EditorGUILayout.IntField("Int Value", 10);
EditorGUILayout.TextField("Text Input", "Hello World");
EditorGUILayout.EndVertical();
}
}
}
运行效果如下