SubjectNerd-Unity / ReorderableInspector

Automatic reorderable lists for Unity game engine components
MIT License
483 stars 48 forks source link
custom-inspectors unity unity3d

Reorderable Inspector

Automatically turn arrays/lists into ReorderableLists in Unity inspectors. Inspired by Alejandro Santiago's implementation.

Sortable Array

This is an editor enhancement that gives you nicer inspector features without having to write additional code. Easily rearrange arrays, add buttons for utility functions, and and edit linked ScriptableObjects right in your GameObject's inspector.

Installation

Download the UnityPackage from the latest releases and import it into Unity. The directory can be moved after being imported.

Usage

To draw an array as a ReorderableList, mark the property with the Reorderable attribute.

// Add this `using` statement to the top of your file
using SubjectNerd.Utilities;

public class ListReorderTest : MonoBehaviour
{  
    [Reorderable]
    public string[] stringArray; // This will be drawn with a ReorderableList

    public List<string> stringList; // This will be drawn as a default array
}

If you want to apply the reorderable list to all arrays, edit ReorderableArrayInspector.cs and uncomment the defines at the top of the file

Additional Features

ContextMenu buttons.

Quickly add buttons for utility functions to the inspector by using Unity's ContextMenu attribute

public class ContextMenuTest : MonoBehaviour
{
    public bool isTestEnabled;

    [ContextMenu("Test Function")]
    private void MyTestFunction()
    {
        Debug.Log("Test function fired");
    }

    [ContextMenu("Test Function", isValidateFunction:true)]
    private bool TestFunctionValidate()
    {
        return isTestEnabled;
    }

    [ContextMenu("Other Test")]
    private void NonValidatedTest()
    {
        Debug.Log("Non validated test fired");
    }
}

Context Menu

Inline ScriptableObject editing

Edit settings stored in a ScriptableObject in the inspector with the EditScriptable attribute. A feature inspired by Tom Kail's ExtendedScriptableObjectDrawer

public class SkinData : ScriptableObject
{
    public string id;
    public Sprite sprite;
}

public class TestEntity : MonoBehaviour
{
    public string entityName;

    // Add the `EditScriptable` attribute to edit the `ScriptableObject` in the GameObject inspector
    [EditScriptable]
    public SkinData skin;
}

Limitations

Custom inspectors

Custom inspectors will not automatically draw arrays as ReorderableLists unless they inherit from ReorderableArrayInspector.

This class contains helper functions that can handle default property drawing. Below is a template for a custom inspector.

Additional custom inspector functionality is discussed in the wiki.

Inspector Template

[CustomEditor(typeof(YourCustomClass))]
public class CustomSortableInspector : ReorderableArrayInspector
{
    // Called by OnEnable
    protected override void InitInspector()
    {
        base.InitInspector();

        // Always call DrawInspector function
        alwaysDrawInspector = true;

        // Do other initializations here
    }

    // Override this function to draw
    protected override void DrawInspector()
    {
        // Call the relevant default drawer functions here
        // The following functions will automatically draw properties
        // with ReorderableList when applicable
        /*
        // Draw all properties
        DrawPropertiesAll();

        // Like DrawPropertiesExcluding
        DrawPropertiesExcept("sprites");

        // Draw all properties, starting from specified property
        DrawPropertiesFrom("propertyName");

        // Draw all properties until before the specified property
        DrawPropertiesUpTo("endPropertyName");

        // Draw properties starting from startProperty, ends before endProperty
        DrawPropertiesFromUpTo("startProperty", "endProperty");
        */

        // Write your custom inspector functions here
        EditorGUILayout.HelpBox("This is a custom inspector", MessageType.Info);
    }
}

Buy me a coffee!

If this Unity enhancement is useful to you, it would be great if you could buy me a coffee!