Coder Social home page Coder Social logo

reorderableinspector's Introduction

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

  • Only supports Unity 5 and above
  • ReorderableLists of class instances may be a little rough, especially below Unity version 5.3
  • Custom inspectors will not automatically gain the ability to turn arrays into reorderable lists. See next section.

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!

reorderableinspector's People

Contributors

chemikhazi avatar galman33 avatar jeiel-bunnyhug avatar mwegner avatar naphier avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.