Serialized property inside custom inspector

Rotorz ReorderableList

Serialized property inside custom inspector

Reorderable list fields can be added to custom inspector interfaces with automatic support for undo/redo when using serialized properties. Serialized properties support native arrays as well as generic lists.

In this example we will implement an editor for the following behaviour class:

using System.Collections.Generic;
using UnityEngine;

public class SomeBehaviour : MonoBehaviour {
    public List<string> wishlist = new List<string>();
}
#pragma strict
import System.Collections.Generic;

var wishlist:List.<String> = new List.<String>();

Custom inspectors can be implemented by extending the Editor Class. The serialized property for our "wishlist" field can then be accessed via the serialize object representation of "SomeBehaviour". We can override the method OnInspectorGUI to present the reorderable list.

using Rotorz.ReorderableList;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(SomeBehaviour))]
public class SomeBehaviourEditor : Editor {

    private SerializedProperty _wishlistProperty;

    void OnEnable() {
        _wishlistProperty = serializedObject.FindProperty("wishlist");
    }

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

        ReorderableListGUI.ListField(_wishlistProperty);

        serializedObject.ApplyModifiedProperties();
    }

}
#pragma strict
import Rotorz.ReorderableList;

@CustomEditor(SomeBehaviour)
class SomeBehaviourEditor extends Editor {

    var _wishlistProperty:SerializedProperty;

    function OnEnable() {
        _wishlistProperty = serializedObject.FindProperty('wishlist');
    }

    function OnInspectorGUI() {
        serializedObject.Update();

        ReorderableListGUI.ListField(_wishlistProperty);

        serializedObject.ApplyModifiedProperties();
    }

}