- Home /
Custom Editor foldout doesn't unfold when clicking on name?
Hi everyone, I implemented my custom Unity editor class with a foldout which shows its child variables when clicked. However, in the inspector, it only unfolds when I click on the little triangle: and not when I click on the actual name itself:
Usually, in Unity's built-in foldouts (for example: Public array variables), clicking on the name automaticallt unfolds it.
Is there any way to get that effect in a custom Unity Editor Script?
Here's my code so far:
isUnfolded = EditorGUILayout.Foldout (isUnfolded , "My Folder");
if (isUnfolded)
//My variables...
Answer by frarees · Apr 11, 2014 at 10:41 AM
Handle the event yourself before EditorGUILayout.Foldout does for you (and Use
it! as shown below)
EditorGUI.BeginChangeCheck ();
EditorGUILayout.GetControlRect (true, 16f, EditorStyles.foldout);
Rect foldRect = GUILayoutUtility.GetLastRect ();
if (Event.current.type == EventType.MouseUp && foldRect.Contains (Event.current.mousePosition)) {
foldout = !foldout;
GUI.changed = true;
Event.current.Use ();
}
foldout = EditorGUI.Foldout (foldRect, foldout, "Foldout");
if (EditorGUI.EndChangeCheck ()) {
Debug.Log ("fold state updated");
}
if (foldout) {
EditorGUI.indentLevel++;
EditorGUILayout.LabelField ("I see you");
EditorGUI.indentLevel--;
}
I've added some extra stuff to mark the foldout as changed, so it's handled properly..
You don't have to handle the event yourself. You just need to call the Foldout version that takes an additional bool (toggleOnLabelClick) between the content and the style parameter. Unfortunately this version isn't "wrapped" for GUILayout. So all you have to do is create the missing wrapping method(s):
public static bool Foldout(bool foldout, GUIContent content, bool toggleOnLabelClick, GUIStyle style)
{
Rect position = GUILayoutUtility.GetRect(40f, 40f, 16f, 16f, style);
// EditorGUI.kNumberW == 40f but is internal
return EditorGUI.Foldout(position, foldout, content, toggleOnLabelClick, style);
}
public static bool Foldout(bool foldout, string content, bool toggleOnLabelClick, GUIStyle style)
{
return Foldout(foldout, new GUIContent(content), toggleOnLabelClick, style);
}
So you just have to pass "true" to "toggleOnLabelClick" like this:
expanded = Foldout(expanded, "someName", true, EditorStyles.foldout);
Like mentioned in the code-comment the original code uses an internal constant "EditorGUI.kNumberW" which has the value 40f but it's internal so you can't access it.
Thanks Bunny83 -- that technique worked perfectly for me too. Wish I could upvote, no rep on this site yet. ;-)
1-line solution: just use EditorGUI version with automatically calculated Rect and toggleOnLabelClick argument set to true:
commonFoldout = EditorGUI.Foldout(EditorGUILayout.GetControlRect(), commonFoldout, "Common issues", true);
@Dmitriy Yukhanov are you sure that always works? I've tried exactly that code but there was an ArgumentException saying: GUILayout: $$anonymous$$ismatched LayoutGroup.Repaint ...
Answer by gresolio · May 04, 2018 at 03:05 PM
I don't know exactly starting with which version this is available, but very handy:
state = EditorGUILayout.Foldout (state, title, true);
or
state = EditorGUILayout.Foldout (state, title, true, EditorStyles.foldout);
Answer by PixelFireXY · Apr 15 at 03:02 PM
Actually, there is a parameter to open and close the foldout by clicking on the label.
Your answer
Follow this Question
Related Questions
Use of the inspector along with a custom editorwindow 1 Answer
Referencing / linking a .asset / .prefab file in another .asset / .prefab file programmatically. 2 Answers
How do you emulate array/List Inspector behavior with an Editor window? 0 Answers
Draw inspector for SerializedObject within EditorWindow? 1 Answer
Set the Rect of PopupWindowContent 1 Answer