Skip to content

Instantly share code, notes, and snippets.

@FriendSea
Last active July 28, 2019 06:40
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 FriendSea/fa6de87af0f2a42e13e16209c95e5a77 to your computer and use it in GitHub Desktop.
Save FriendSea/fa6de87af0f2a42e13e16209c95e5a77 to your computer and use it in GitHub Desktop.
配列ぜんぶReorderableListにする
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