【第一章编辑器开发基础第一节绘制编辑器元素_7折叠面板控件(7/7)】

折叠面板控件

​​用途​​:组织/隐藏复杂UI区块

​​所属类​​:EditorGUILayout

​​调用位置​​:OnInspectorGUI()

​​标准实现​​:

csharp 复制代码
foldout = EditorGUILayout.Foldout(foldout, "面板标题", true);

if(foldout) {
    // 展开状态下的内容
    GUILayout.Button("子控件");
}

​​使用技巧​​:

第三个参数控制是否有展开动画

面板内可嵌套任意UI元素

状态管理需开发者自行维护

特别适合组织同类配置项

​​记忆口诀​​:

"折叠面板三件套:状态变量、Foldout调用、if包裹内容"

示例代码和效果图

csharp 复制代码
using UnityEngine;
using UnityEditor;

/// <summary>
/// 创建自定义编辑器,作用于Example组件
/// </summary>
[CustomEditor(typeof(Example))]
public class ExampleEditor : Editor
{


    private bool foldout1; // 折叠面板1的状态
    private bool foldout2; // 折叠面板2的状态

    // ================= 核心方法 - 绘制编辑器界面 =================
    public override void OnInspectorGUI()
    {
        // 1. 先绘制默认Inspector内容
        base.OnInspectorGUI();

        // 2. 依次绘制各种自定义控件

        Foldout();     // 折叠面板


      
    }


   

    // ================= 7. 折叠面板控件 =================
    private void Foldout()
    {
        // 折叠面板1(带展开/收起动画)
        foldout1 = EditorGUILayout.Foldout(foldout1, "Foldout1", true);
        if (foldout1) // 展开状态时显示内容
        {
            GUILayout.Label("Hello world", EditorStyles.miniBoldLabel);
            GUILayout.Label("Hello world", EditorStyles.boldLabel);
            GUILayout.Label("Hello world", EditorStyles.largeLabel);
        }

        // 折叠面板2
        foldout2 = EditorGUILayout.Foldout(foldout2, "Foldout2", true);
        if (foldout2) // 展开状态时显示内容
        {
            GUILayout.Button("btn1");
            GUILayout.Button("btn2");
            GUILayout.Button("btn3");
        }
    }


}
相关推荐
m0_555762904 小时前
VSCODE CLAUDE CODE
ide·vscode·编辑器
esmap9 小时前
ESMAP 智慧消防解决方案:以数字孪生技术构建全域感知消防体系,赋能消防安全管理智能化升级
人工智能·物联网·3d·编辑器·智慧城市
大明者省10 小时前
激活函数选型速查表(核心规则)
编辑器
GitHubDaily11 小时前
Windows 欠了 30 年的那个原生编辑器,终于回来了。
编辑器
蓝丶曦月13 小时前
MacM系列芯片安装 最新版本Notepad--(平替Windows系统的Notepad++)详细教程
编辑器·notepad++·mac
jun_bai13 小时前
VSCode使用
ide·vscode·编辑器
CaracalTiger17 小时前
OpenClaw-VSCode:在 VS Code 中通过 WebSocket 远程管理 OpenClaw 网关的完整方案
运维·ide·人工智能·vscode·websocket·开源·编辑器
Web极客码19 小时前
WordPress从经典编辑器升级到古腾堡编辑器
运维·编辑器·wordpress
江湖有缘19 小时前
Docker部署music-tag-web音乐标签编辑器
前端·docker·编辑器
LYOBOYI1232 天前
vscode界面美化
ide·vscode·编辑器