Last active
July 28, 2019 06:40
-
-
Save FriendSea/fa6de87af0f2a42e13e16209c95e5a77 to your computer and use it in GitHub Desktop.
配列ぜんぶReorderableListにする
This file contains 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.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEditorInternal; | |
[CustomEditor(typeof(UnityEngine.Object), true)] | |
[CanEditMultipleObjects] | |
public class ReorderableEditor : Editor | |
{ | |
List<ReorderableList> reorderableLists = new List<ReorderableList>(); | |
ReorderableList CreateReordable(SerializedProperty property) | |
{ | |
var reorderableList = new ReorderableList(property.serializedObject, property); | |
reorderableList.drawElementCallback += (Rect rect, int index, bool isActive, bool isFocused) => | |
{ | |
var prop = property.GetArrayElementAtIndex(index); | |
EditorGUI.PropertyField(rect, prop, true); | |
}; | |
reorderableList.elementHeightCallback += index => | |
{ | |
var prop = property.GetArrayElementAtIndex(index); | |
return EditorGUI.GetPropertyHeight(prop); | |
}; | |
reorderableList.drawHeaderCallback += rect => | |
{ | |
EditorGUI.LabelField(rect, property.displayName); | |
}; | |
return reorderableList; | |
} | |
bool isList(SerializedProperty property) | |
{ | |
if (property.propertyType == SerializedPropertyType.String) | |
return false; | |
return property.isArray; | |
} | |
void OnEnable() | |
{ | |
reorderableLists.Clear(); | |
var iter = serializedObject.GetIterator(); | |
if (!iter.NextVisible(true)) | |
return; | |
while (iter.NextVisible(false)) | |
{ | |
if (!isList(iter)) continue; | |
reorderableLists.Add(CreateReordable(iter.Copy())); | |
} | |
} | |
public override void OnInspectorGUI() | |
{ | |
if (reorderableLists.Count == 0) | |
{ | |
base.OnInspectorGUI(); | |
return; | |
} | |
int index = 0; | |
serializedObject.Update(); | |
var iter = serializedObject.GetIterator(); | |
iter.NextVisible(true); | |
using (new EditorGUI.DisabledGroupScope(true)) | |
EditorGUILayout.PropertyField(iter); | |
while (iter.NextVisible(false)) | |
{ | |
if (isList(iter)) | |
{ | |
reorderableLists[index].DoLayoutList(); | |
index++; | |
} | |
else | |
EditorGUILayout.PropertyField(iter); | |
} | |
serializedObject.ApplyModifiedProperties(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment