Skip to content

Custom Inspectors

Jeiel Aranal edited this page Jul 5, 2017 · 2 revisions

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

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

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);
	}
}

Extending ReorderableList Functionality

By default, the automatic ReorderableLists handle the minimal of array management. They can be further extended by adding your own callbacks. As of Unity 5.6, there is no official documentation yet, this post has extensive documentation of the callbacks you can add to ReorderableList

The drag and drop handler for a list can also be set for handling dragging and dropping into lists of custom classes.

// SerializableObject
public class YourCustomClass : SerializableObject
{
	[Reorderable]
	public List<GameObject> enemyPrefabs;
	
	[Serializable]
	public struct AudioData
	{
		public AudioClip clip;
		public float volume;
	}
	
	[Reorderable]
	public List<AudioData> audioList;
}
// Custom inspector
[CustomEditor(typeof(YourCustomClass))]
public class CustomSortableInspector : ReorderableArrayInspector
{
	// Called by OnEnable
	protected override void InitInspector()
	{
		base.InitInspector();
		
		// Get enemy prefabs list property
		SerializedProperty propList = serializedObject.FindProperty("enemyPrefabs");
		
		// Modify the callbacks of the ReorderableList here. Refer here for details
		// http://va.lent.in/unity-make-your-lists-functional-with-reorderablelist/
		//
		// Ideas:
		// Add a custom add dropdown, change how the elements are drawn, handle removing or adding objects
		ReorderableList listEnemies = GetSortableList(propList);
		
		// Get audio list property
		SerializedProperty propAudio = serializedObject.FindProperty("audioList");
		
		// Set the drag and drop handling function for the audio list
		SetDragDropHandler(propAudio, HandleAudioDragDrop);
	}
	
	protected void HandleAudioDragDrop(SerializedProperty propList, UnityEngine.Object[] objects)
	{
		bool didAdd = false;
		// Process the list of objects being drag and dropped into the audio list
		foreach (Object obj in objects)
		{
			AudioClip clip = obj as AudioClip;
			if (clip == null)
				continue;
			didAdd = true;
			
			// When list is expanded, current array size is the last index
			int newIdx = property.arraySize;
			// Expand list size
			property.arraySize++;
			
			// Get the last array element
			var propData = property.GetArrayElementAtIndex(newIdx);
			// And set the data
			propData.FindPropertyRelative("clip").objectReferenceValue = clip;
			propData.FindPropertyRelative("volume").floatValue = 1f;
		}
		
		// Make sure to apply modified properties
		if (didAdd)
			property.serializedObject.ApplyModifiedProperties();
	}
}
Clone this wiki locally