| | 1 | | using System.Collections.Generic; |
| | 2 | | using UnityEditor; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace ReorderableList.Editor |
| | 6 | | { |
| | 7 | |
|
| | 8 | | [CustomPropertyDrawer(typeof(ReorderableAttribute))] |
| | 9 | | public class ReorderableDrawer : PropertyDrawer |
| | 10 | | { |
| | 11 | |
|
| | 12 | | public const string ARRAY_PROPERTY_NAME = "array"; |
| | 13 | |
|
| 0 | 14 | | private static Dictionary<int, ReorderableList> lists = new Dictionary<int, ReorderableList>(); |
| | 15 | |
|
| 0 | 16 | | public override bool CanCacheInspectorGUI(SerializedProperty property) { return false; } |
| | 17 | |
|
| | 18 | | public override float GetPropertyHeight(SerializedProperty property, GUIContent label) |
| | 19 | | { |
| | 20 | |
|
| 0 | 21 | | ReorderableList list = GetList(property, attribute as ReorderableAttribute, ARRAY_PROPERTY_NAME); |
| | 22 | |
|
| 0 | 23 | | return list != null ? list.GetHeight() : EditorGUIUtility.singleLineHeight; |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) |
| | 27 | | { |
| | 28 | |
|
| 0 | 29 | | ReorderableList list = GetList(property, attribute as ReorderableAttribute, ARRAY_PROPERTY_NAME); |
| | 30 | |
|
| 0 | 31 | | if (list != null) |
| | 32 | | { |
| | 33 | |
|
| 0 | 34 | | list.DoList(EditorGUI.IndentedRect(position), label); |
| 0 | 35 | | } |
| | 36 | | else |
| | 37 | | { |
| | 38 | |
|
| 0 | 39 | | GUI.Label(position, "Array must extend from ReorderableArray", EditorStyles.label); |
| | 40 | | } |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | public static int GetListId(SerializedProperty property) |
| | 44 | | { |
| | 45 | |
|
| 0 | 46 | | if (property != null) |
| | 47 | | { |
| | 48 | |
|
| 0 | 49 | | int h1 = property.serializedObject.targetObject.GetHashCode(); |
| 0 | 50 | | int h2 = property.propertyPath.GetHashCode(); |
| | 51 | |
|
| 0 | 52 | | return (((h1 << 5) + h1) ^ h2); |
| | 53 | | } |
| | 54 | |
|
| 0 | 55 | | return 0; |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | public static ReorderableList GetList(SerializedProperty property, string arrayPropertyName) { return GetList(pr |
| | 59 | |
|
| 0 | 60 | | public static ReorderableList GetList(SerializedProperty property, ReorderableAttribute attrib, string arrayProp |
| | 61 | |
|
| 0 | 62 | | public static ReorderableList GetList(SerializedProperty property, int id, string arrayPropertyName) { return Ge |
| | 63 | |
|
| | 64 | | public static ReorderableList GetList(SerializedProperty property, ReorderableAttribute attrib, int id, string a |
| | 65 | | { |
| | 66 | |
|
| 0 | 67 | | if (property == null) |
| | 68 | | { |
| | 69 | |
|
| 0 | 70 | | return null; |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | ReorderableList list = null; |
| 0 | 74 | | SerializedProperty array = property.FindPropertyRelative(arrayPropertyName); |
| | 75 | |
|
| 0 | 76 | | if (array != null && array.isArray) |
| | 77 | | { |
| | 78 | |
|
| 0 | 79 | | if (!lists.TryGetValue(id, out list)) |
| | 80 | | { |
| | 81 | |
|
| 0 | 82 | | if (attrib != null) |
| | 83 | | { |
| | 84 | |
|
| 0 | 85 | | Texture icon = !string.IsNullOrEmpty(attrib.elementIconPath) ? AssetDatabase.GetCachedIcon(attri |
| | 86 | |
|
| 0 | 87 | | ReorderableList.ElementDisplayType displayType = attrib.singleLine ? ReorderableList.ElementDisp |
| | 88 | |
|
| 0 | 89 | | list = new ReorderableList(array, attrib.add, attrib.remove, attrib.draggable, displayType, attr |
| 0 | 90 | | list.paginate = attrib.paginate; |
| 0 | 91 | | list.pageSize = attrib.pageSize; |
| 0 | 92 | | list.sortable = attrib.sortable; |
| | 93 | |
|
| | 94 | | //handle surrogate if any |
| | 95 | |
|
| 0 | 96 | | if (attrib.surrogateType != null) |
| | 97 | | { |
| | 98 | |
|
| 0 | 99 | | SurrogateCallback callback = new SurrogateCallback(attrib.surrogateProperty); |
| | 100 | |
|
| 0 | 101 | | list.surrogate = new ReorderableList.Surrogate(attrib.surrogateType, callback.SetReference); |
| | 102 | | } |
| 0 | 103 | | } |
| | 104 | | else |
| | 105 | | { |
| | 106 | |
|
| 0 | 107 | | list = new ReorderableList(array, true, true, true); |
| | 108 | | } |
| | 109 | |
|
| 0 | 110 | | lists.Add(id, list); |
| 0 | 111 | | } |
| | 112 | | else |
| | 113 | | { |
| | 114 | |
|
| 0 | 115 | | list.List = array; |
| | 116 | | } |
| | 117 | | } |
| | 118 | |
|
| 0 | 119 | | return list; |
| | 120 | | } |
| | 121 | |
|
| | 122 | | private struct SurrogateCallback |
| | 123 | | { |
| | 124 | |
|
| | 125 | | private string property; |
| | 126 | |
|
| 0 | 127 | | internal SurrogateCallback(string property) { this.property = property; } |
| | 128 | |
|
| | 129 | | internal void SetReference(SerializedProperty element, Object objectReference, ReorderableList list) |
| | 130 | | { |
| | 131 | |
|
| 0 | 132 | | SerializedProperty prop = !string.IsNullOrEmpty(property) ? element.FindPropertyRelative(property) : nul |
| | 133 | |
|
| 0 | 134 | | if (prop != null && prop.propertyType == SerializedPropertyType.ObjectReference) |
| | 135 | | { |
| | 136 | |
|
| 0 | 137 | | prop.objectReferenceValue = objectReference; |
| | 138 | | } |
| 0 | 139 | | } |
| | 140 | | } |
| | 141 | | } |
| | 142 | | } |