1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| using UnityEditor; using UnityEditorInternal; using UnityEngine; [CustomEditor(typeof(Test))] public class TestEditor : Editor { SerializedProperty itemOneList; ReorderableList reorderableOneList; SerializedProperty itemTwoList; ReorderableList reorderableTwoList; readonly float space = EditorGUIUtility.standardVerticalSpacing; readonly float foldHeight = 15; readonly float amountElementHeight = 20; void OnEnable() { itemOneList = serializedObject.FindProperty("itemOneList"); reorderableOneList = new ReorderableList(serializedObject, itemOneList, true, true, true, true); reorderableOneList.drawHeaderCallback = DrawOneListHeader; reorderableOneList.drawElementCallback = DrawOneListElement; reorderableOneList.elementHeightCallback = OneListElementHeight; itemTwoList = serializedObject.FindProperty("itemTwoList"); reorderableTwoList = new ReorderableList(serializedObject, itemTwoList, true, true, true, true); reorderableTwoList.drawHeaderCallback = DrawTwoListHeader; reorderableTwoList.drawElementCallback = DrawTwoListElement; reorderableTwoList.elementHeightCallback = TwoListElementHeight; } void DrawOneListHeader(Rect rect) { EditorGUI.LabelField(rect, "ItemOneList"); } void DrawTwoListHeader(Rect rect) { EditorGUI.LabelField(rect, "ItemTwoList"); } void DrawOneListElement(Rect rect, int index, bool selected, bool focused) { SerializedProperty element = itemOneList.GetArrayElementAtIndex(index); rect.y += space; SerializedProperty name = element.FindPropertyRelative("name"); EditorGUI.PropertyField(new Rect(rect.x, rect.y, Screen.width * .8f, EditorGUI.GetPropertyHeight(name)), name); SerializedProperty objs = element.FindPropertyRelative("objs"); EditorGUI.PropertyField(new Rect(rect.x + 12, rect.y + EditorGUI.GetPropertyHeight(name), Screen.width * .8f, EditorGUI.GetPropertyHeight(objs, true)), objs, true); } void DrawTwoListElement(Rect rect, int index, bool selected, bool focused) { SerializedProperty element = itemTwoList.GetArrayElementAtIndex(index); rect.y += space; SerializedProperty trigger = element.FindPropertyRelative("trigger"); float triggerHeight = EditorGUI.GetPropertyHeight(trigger); EditorGUI.PropertyField(new Rect(rect.x, rect.y, Screen.width * .8f, triggerHeight), trigger); SerializedProperty fold = element.FindPropertyRelative("fold"); fold.boolValue = EditorGUI.Foldout(new Rect(rect.x + 12, rect.y + triggerHeight, Screen.width * .8f, foldHeight), fold.boolValue, "Amounts", true); if (fold.boolValue) { SerializedProperty types = element.FindPropertyRelative("types"); SerializedProperty amounts = element.FindPropertyRelative("amounts"); types.arraySize = amounts.arraySize = reorderableOneList.count; for (int i = 0; i < types.arraySize; i++) { SerializedProperty typeElement = types.GetArrayElementAtIndex(i); SerializedProperty amountElement = amounts.GetArrayElementAtIndex(i); SerializedProperty oneListElement = itemOneList.GetArrayElementAtIndex(i); SerializedProperty name = oneListElement.FindPropertyRelative("name"); typeElement.stringValue = name.stringValue; EditorGUI.LabelField(new Rect(rect.x + 12, rect.y + triggerHeight + foldHeight + amountElementHeight * i + space * (i + 1), Screen.width * .3f, amountElementHeight), typeElement.stringValue); amountElement.floatValue = EditorGUI.Slider(new Rect(rect.x + 12 + Screen.width * .3f, rect.y + triggerHeight + foldHeight + amountElementHeight * i + space * (i + 1), Screen.width * .5f, amountElementHeight), amountElement.floatValue, 0, 1); } } } float OneListElementHeight(int index) { SerializedProperty element = itemOneList.GetArrayElementAtIndex(index); float height = space; SerializedProperty name = element.FindPropertyRelative("name"); SerializedProperty objs = element.FindPropertyRelative("objs"); height += EditorGUI.GetPropertyHeight(name) + EditorGUI.GetPropertyHeight(objs, true); return height + space; } float TwoListElementHeight(int index) { SerializedProperty element = itemTwoList.GetArrayElementAtIndex(index); float height = space; SerializedProperty trigger = element.FindPropertyRelative("trigger"); SerializedProperty amounts = element.FindPropertyRelative("amounts"); SerializedProperty fold = element.FindPropertyRelative("fold"); float triggerHeight = EditorGUI.GetPropertyHeight(trigger); float amountHeight = (amountElementHeight + space) * amounts.arraySize; height += triggerHeight + foldHeight + (fold.boolValue ? amountHeight : 0f); return height + space; } public override void OnInspectorGUI() { serializedObject.Update(); reorderableOneList.DoLayoutList(); reorderableTwoList.DoLayoutList(); serializedObject.ApplyModifiedProperties(); } }
|