Created
May 23, 2017 12:19
-
-
Save ashblue/ea4be4f07d1f11117543e053130d8e4f to your computer and use it in GitHub Desktop.
Create a sortable list with this re-usable class on the fly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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