List all serialized properties in a custom inspector

I often find my self wanting to add a button or some other simple control to an inspector for a component that already exposes a lot of public properties. I'll show you how you can append controls to an inspector without having to write lots of code.

This is my test component:

using UnityEngine;
using System.Collections;

public class MyComp : MonoBehaviour {

    public bool     activate = false;
    public int      level = 2;
    public float    balance = 0.5f;

}

To make the properties appear in a custom inspector, we could add them manually one by one, like this:

using UnityEditor;
using UnityEngine;
using System.Collections;

[CustomEditor(typeof(MyComp))]
public class MyCompEditor : Editor {

    private SerializedProperty  activate;
    private SerializedProperty  level;
    private SerializedProperty  balance;

    void OnEnable() {
        activate = serializedObject.FindProperty("activate");
        level = serializedObject.FindProperty("level");
        balance = serializedObject.FindProperty("balance");
    }

    override public void OnInspectorGUI() {
        serializedObject.Update();

        EditorGUILayout.PropertyField(activate);
        EditorGUILayout.PropertyField(level);
        EditorGUILayout.PropertyField(balance);

        if (GUI.changed) {
            serializedObject.ApplyModifiedProperties();
        }
    }
}

That will not scale well. What happens when you add a new property to MyComp? You have to add a bunch of lines to your inspector too. There is an easier way!

using UnityEditor;
using UnityEngine;
using System.Collections;

[CustomEditor(typeof(MyComp))]
public class MyCompEditor : Editor {

    private SerializedObject    so;
    private SerializedProperty  prop;

    void OnEnable() {
        so = new SerializedObject(target);
    }

    override public void OnInspectorGUI() {
        prop = so.GetIterator();
        while (prop.NextVisible(true)) {
            EditorGUILayout.PropertyField(prop);
        }

        EditorGUILayout.Space();

        if (GUILayout.Button("Woop!")) {
            Debug.Log("Woop!");
        }

        if (GUI.changed) {
            so.ApplyModifiedProperties();
        }
    }
}

Check it out, yo! We iterated over the properties of the serialized object. And then we added a button. Whenever you add a new property to your component, your inspector will update as well.

An even simpler solution

Emil Johansen (@AngryAnt) just pointed out that you could just add Editor.DrawDefaultInspector() in the OnInspectorGUI function, like this:

using UnityEditor;
using UnityEngine;
using System.Collections;

[CustomEditor(typeof(MyComp))]
public class MyCompEditor : Editor {

    override public void OnInspectorGUI() {
        DrawDefaultInspector();

        EditorGUILayout.Space();

        if (GUILayout.Button("Woop!")) {
            Debug.Log("Woop!");
        }

        if (GUI.changed) {
            so.ApplyModifiedProperties();
        }
    }
}

Latest articles