Adding Reorderable Lists in Unity


Often I find myself in need of reorderable list functionality in my various custom editor interfaces, and so I decided to create a generalised implementation which works with generic lists List<T> and with SerializedProperty.

Screenshot of Reorderable List Field

This list control provides drag and drop reordering and supports mixed item heights when using custom property drawers. It is generally better to present lists for serialized properties since Unity provides automatic undo/redo support for these. You can also implement your very own list adaptor to present entirely custom lists.

This control is provided as open source under the BSD license and can be downloaded from the following repository: https://bitbucket.org/rotorz/reorderable-list-editor-field-for-unity/overview

Here is the custom editor which is used in the above screenshot:

SerializedProperty _shoppingListProperty;
SerializedProperty _purchasedListProperty;

void OnEnable() {
    _shoppingListProperty = serializedObject.FindProperty("shoppingList");
    _purchasedListProperty = serializedObject.FindProperty("purchasedList");
}

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

    ReorderableListGUI.Title("Shopping List");
    ReorderableListGUI.ListField(_shoppingListProperty);

    ReorderableListGUI.Title("Purchased Items");
    ReorderableListGUI.ListField(_purchasedListProperty,
        ReorderableListFlags.DisableReordering
      | ReorderableListFlags.HideAddButton
    );

    serializedObject.ApplyModifiedProperties();
}
Advertisements

3 thoughts on “Adding Reorderable Lists in Unity

  1. @Lea: Do you think it’s possible to turn this into a PropertyDrawer? Or does it already have a PropertyDrawer version? Mainly looking into that so I wouldn’t have to make Editor classes for the scripts I want to use this on.

    1. Sadly since Unity 4.3 property drawers cannot be applied to the outer collection of an array, they can only be applied per element. So the answer is, not automatically.

      You can create a custom property drawer for a custom attribute and then draw the reorderable list using the ReorderableListGUI functions which I have provided. Or of course you can just draw the reorderable list within a custom inspector.

      I hope that this helps!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s