EditorGUI.indentLevel

EditorGUI.indentLevel is a property provided by the UnityEditor namespace in Unity that allows you to control the indentation level of GUI elements within a custom editor.

When you create a custom editor for your script and override the OnInspectorGUI method, you can use EditorGUI.indentLevel to control the visual indentation of GUI elements. This property affects how the elements are visually aligned and nested within the inspector.

Here are some key points to understand about EditorGUI.indentLevel:

1. **Indentation Levels**: The EditorGUI.indentLevel property is an integer value that represents the current indentation level. By default, it is set to 0, indicating no indentation.

2. **Hierarchy of Indentation**: Each increment of EditorGUI.indentLevel represents an additional level of indentation. For example, when EditorGUI.indentLevel is set to 1, the GUI elements within the OnInspectorGUI method will be indented to create a visual hierarchy.

3. **Nested Elements**: The indentation level affects how nested GUI elements are displayed. For example, if you have a group of related GUI elements that should appear indented to indicate a nested relationship, you can increase the EditorGUI.indentLevel before rendering them and decrease it afterward.

4. **Using EditorGUI.indentLevel**: To adjust the indentation level, you can increment or decrement the EditorGUI.indentLevel property using the ++ and -- operators, respectively. This property affects subsequent GUI elements rendered using EditorGUILayout or EditorGUI methods.

public override void OnInspectorGUI()
{
   EditorGUI.indentLevel++; // Increase indentation level
   EditorGUI.indentLevel--; // Decrease indentation level
}

EditorGUI.indentLevel provides you with control over the visual layout and organization of GUI elements within the Unity inspector, allowing you to create a clear and structured interface for your custom editor.

How is the default value of EditorGUI.indentLevel defined?

By default, the value of EditorGUI.indentLevel is 0. It’s important to note that this property is static, which means it’s shared among all instances of EditorGUI during the execution of the editor code.

The main purpose of this property is to control the amount of indentation for the layout of editor GUIs, which can be very useful when creating complex and nested editors.

Here is an example of how you can use EditorGUI.indentLevel:

EditorGUI.indentLevel++;
EditorGUILayout.LabelField("I am indented");
EditorGUI.indentLevel--;
EditorGUILayout.LabelField("I am not indented");

This example will render two labels, the first indented and the second not indented.

Remember, when increasing EditorGUI.indentLevel, it’s good practice to decrement it back after you’re done. This way, you won’t inadvertently affect other parts of your editor code.

What happens when you increase the value of EditorGUI.indentLevel?

The EditorGUI.indentLevel property controls the amount of indentation applied to the controls drawn with the EditorGUI class in Unity.

When you increase the value of EditorGUI.indentLevel, you add a level of indentation to the Unity Editor GUI controls that you draw. This indentation is applied from the left side, pushing the control’s position further to the right.

This is often used to logically group and structure the controls in a custom editor, to make it easier to read and understand. For example, properties of a nested object could be indented to show that they belong to the nested object.

Here’s a quick example:

EditorGUI.indentLevel++;
EditorGUILayout.LabelField("This label is indented");
EditorGUI.indentLevel--;
EditorGUILayout.LabelField("This label is not indented");

In this example, the first label field will appear indented (or pushed to the right) in the Editor, indicating that it belongs to a different logical group than the second label field.

Can you provide an example of how EditorGUI.indentLevel can be used to structure GUI elements?

Here’s a simple example of how EditorGUI.indentLevel can be used to structure GUI elements in a custom Editor. In this example, we’ll create an editor for a hypothetical “Character” object with “Stats” and “Abilities” subcategories.

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(Character))]
public class CharacterEditor : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();

EditorGUILayout.PropertyField(serializedObject.FindProperty("characterName"));

EditorGUI.indentLevel++;
EditorGUILayout.LabelField("Stats");
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(serializedObject.FindProperty("health"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("strength"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("intelligence"));
EditorGUI.indentLevel--;

EditorGUILayout.LabelField("Abilities");
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(serializedObject.FindProperty("ability1"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("ability2"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("ability3"));
EditorGUI.indentLevel--;

EditorGUI.indentLevel--;

serializedObject.ApplyModifiedProperties();
}
}

In this example, we have a custom editor for a hypothetical Character class. The OnInspectorGUI method is overridden to provide a custom layout for the inspector.

The characterName field is displayed first without any indentation.

The EditorGUI.indentLevel++ statement increases the indentation level, pushing the following controls to the right. We then display a label “Stats” and further increase the indentation level for the character’s stats properties – health, strength, intelligence.

We then do the same for a set of “Abilities” – ability1, ability2, ability3.

Each time after we finish displaying a group of related properties, we decrease the indentation level using EditorGUI.indentLevel--.

The end result is that the “Stats” and “Abilities” fields will be indented under “characterName”, and the individual stats and abilities will be further indented under their respective headers, creating a clear, hierarchical visual structure in the Editor.

Is it possible to nest multiple levels of indentation using EditorGUI.indentLevel?

Yes, it’s possible to nest multiple levels of indentation using EditorGUI.indentLevel. Each increment of EditorGUI.indentLevel will add another level of indentation, pushing the controls further to the right. This can be used to create a hierarchical visual structure in the editor.

Here is a simple example:

using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(MyScriptableObject))]
public class MyScriptableObjectEditor : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();

EditorGUILayout.PropertyField(serializedObject.FindProperty("topLevelProperty"));

EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(serializedObject.FindProperty("secondLevelProperty1"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("secondLevelProperty2"));

EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(serializedObject.FindProperty("thirdLevelProperty1"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("thirdLevelProperty2"));

EditorGUI.indentLevel--;
EditorGUILayout.PropertyField(serializedObject.FindProperty("secondLevelProperty3"));

EditorGUI.indentLevel--;
EditorGUILayout.PropertyField(serializedObject.FindProperty("topLevelProperty2"));

serializedObject.ApplyModifiedProperties();
}
}

In this example, the properties will be displayed as follows:

topLevelProperty is displayed with no indentation.
secondLevelProperty1 and secondLevelProperty2 are displayed with one level of indentation.
thirdLevelProperty1 and thirdLevelProperty2 are displayed with two levels of indentation.
secondLevelProperty3 is displayed with one level of indentation, because we have decreased the indent level by one.
topLevelProperty2 is displayed with no indentation, because we have decreased the indent level by one more.

Remember to always decrease the indentation level back to its original value when you’re done using it, to prevent other controls from being unintentionally indented.

Related Articles

Responses

Your email address will not be published. Required fields are marked *