- Home /
EditorGUI.Foldout consumes click so GUI.Button doesnt work when inside of foldout region.
I am writing a custom property drawer, and I have a EditorGUI.Foldout which when unfolded has inside a GUI.Button. It turns out the button doesnt receive any clicks, cause the Foldout region consumes its click event. Any workaround?, or anything I can do to make clicks to be received by the button inside the foldout region?
Answer by lulitd · Jul 25, 2017 at 07:10 PM
I recently encountered this problem.
I solved it by adjusting the height of the rect I pass in. The area that is clickable for the foldout is determined by the rect. I made it so that only the height of the label is clickable.
private const float buttonSize= 45F;
private bool showFoldout = false;
public override void OnGUI(Rect container, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(container, label, property);
// set the width and/or height of the rect that you pass into the foldout to not cover your buttons.
// the orginal container.height uses the get property height.
container.height = EditorGUIUtility.singleLineHeight;
showFoldout = EditorGUI.Foldout(container, showFoldout, label, false, EditorStyles.foldout);
if (showFoldout)
{
// insert ur layout here.
}
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return (showFoldout)?buttonHeight* 3f + EditorGUIUtility.singleLineHeight * 3 : EditorGUIUtility.singleLineHeight;
}
Thank you! I was banging my head against this custom inspector that wouldn't let me focus the input fields inside a foldout. Turns out the foldout rect height needs to be set to single line, otherwise it will invisibly cover its contents and make them unresponsive.
rect.height = EditorGUIUtility.singleLineHeight;
property.isExpanded = EditorGUI.Foldout(rect, property.isExpanded, label);
Cheers!