Unity编辑器:GUILayout未显示在FoldoutHeaderGroup内的问题求助
问题解决:让Label显示在FoldoutHeaderGroup内
你的问题出在没有根据折叠组的展开状态控制内容显示,也没处理内容缩进。EditorGUILayout.BeginFoldoutHeaderGroup只是创建折叠头部,内部内容需要你手动判断折叠状态来决定是否渲染,还要手动添加缩进对齐。
修正后的代码如下:
using UnityEditor; using UnityEngine; public class tesdtEditor : EditorWindow { private bool showWindowFoldOut; [MenuItem("test")] public static void ShowWindow() { EditorWindow.GetWindow(typeof(tesdtEditor)); } public void OnGUI() { showWindowFoldOut = EditorGUILayout.BeginFoldoutHeaderGroup(showWindowFoldOut, "foldout Name"); // 只有折叠组展开时才渲染内部内容 if (showWindowFoldOut) { // 增加缩进,让内容和折叠头对齐 EditorGUI.indentLevel++; GUILayout.Label("title"); EditorGUI.indentLevel--; } EditorGUILayout.EndFoldoutHeaderGroup(); } }
关键修改说明:
- 折叠状态判断:通过
if(showWindowFoldOut)包裹内部UI元素,确保只有折叠组展开时才显示内容。 - 缩进处理:用
EditorGUI.indentLevel增加/减少缩进,让折叠内容和头部形成视觉层级,符合Unity编辑器的交互习惯。
这样修改后,你的标签就会正确显示在折叠组内部,展开时可见,折叠时隐藏,且排版对齐。
内容的提问来源于stack exchange,提问作者Isaac
相关产品推荐
相关产品推荐

