- Home /
How to properly deal with EditorGUI.indentLevel in custom PropertyDrawers
If I have the following code snippet in a CustomEditor
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(testProperty);
EditorGUI.indentLevel--;
If the testProperty’s CustomPropertyDrawer uses say..
flag=EditorGUI.Foldout(position,flag,…)
Then the graphical layout for the foldout button does NOT match the clickable area of the foldout button. I need to click to the LEFT of my visible foldout button, in order for the click to be detected.
Why does EditorGUI.indentLevel>0
cause such a misalignment between the drawn location vs. the clickable location, and what is the proper way to deal with this?
Do I actually need to manually/temporarily override the EditorGUI.indetLevel
value, and recompute the “position” Rect, that is passed to my CustomPropertyDrawer , like so? (I feel like this should be happening for me, automatically. In fact, it seems like it halfway IS.)
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
int recordedIndentLevel=EditorGUI.indentLevel;
position.xMin += EditorGUI.indentLevel * 10.0f; //FYI: 10.0f is incorrect, not sure yet how to get actual indent size
EditorGUI.indentLevel = 0;
//draw stuff using position rect
flag=EditorGUI.Foldout(position,flag,…)
EditorGUI.indentLevel = recordedIndentLevel;
}
Answer by Ash-Blue · Sep 06, 2017 at 08:59 PM
Looks like you need to call var indentedRect = EditorGUI.IndentedRect(position);
to get the indent you're looking for.
This is correct. However, one must ALSO set the indentLevel to 0, before calling foldout. In addition, to get the foldout in the ACTUAL indented rect (as opposed to just to the left of it), and have it heed the foldout function's allowClickOnLabel parameter, one must ALSO set https://docs.unity3d.com/ScriptReference/EditorGUIUtility-hierarchy$$anonymous$$ode.html to false.
THANKS! EditorGUI.IndentedRect
was fixing most of my issue, but ALSO adding EditorGUI.indentLevel = 0
before my foldout display has completely fixed it!
Your answer
Follow this Question
Related Questions
PropertyDrawer inserting large space for list 0 Answers
Draw on Multiple Lines with EditorGUI 1 Answer
Horizontal Layout Group making a Scroll Rect's Content rect width to be negative 1 Answer
Fill empty space in parent in UI Layout 1 Answer
Translate GetWorldCorners (new UI) to Viewport for a camera rect 1 Answer