Skip to content

Instantly share code, notes, and snippets.

@ashblue
Created May 23, 2017 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashblue/ea4be4f07d1f11117543e053130d8e4f to your computer and use it in GitHub Desktop.
Save ashblue/ea4be4f07d1f11117543e053130d8e4f to your computer and use it in GitHub Desktop.
Create a sortable list with this re-usable class on the fly.
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
namespace Adnc.Pathfinding2D.Utility {
/// <summary>
/// Usage example:
/// [CustomEditor(typeof(MyBaseClass))]
/// public class MyBaseClassEditor : Editor {
/// private SortableList buildTasks;
///
/// void OnEnable () {
/// buildTasks = new SortableList(this, "mySerializedProp", "Title of list");
/// }
///
/// public override void OnInspectorGUI() {
/// DrawDefaultInspector();
/// serializedObject.Update();
///
/// if (buildTasks.list != null) {
/// buildTasks.list.DoLayoutList();
/// }
///
/// serializedObject.ApplyModifiedProperties();
/// }
/// }
/// </summary>
public class SortableList {
public ReorderableList list;
public SortableList (Editor editor, string property, string title) {
var prop = editor.serializedObject.FindProperty(property);
if (prop == null) {
Debug.LogErrorFormat("Could not find property {0}", property);
return;
}
list = new ReorderableList(editor.serializedObject,
editor.serializedObject.FindProperty(property),
true, true, true, true);
list.drawElementCallback =
(rect, index, isActive, isFocused) => {
var element = list.serializedProperty.GetArrayElementAtIndex(index);
rect.y += 2;
EditorGUI.PropertyField(
new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight),
element, GUIContent.none);
};
list.drawHeaderCallback = rect => { EditorGUI.LabelField(rect, title); };
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment