Created
December 24, 2019 08:35
-
-
Save hananoki/9aeeeb1ba347204e4f90a4f825760fb4 to your computer and use it in GitHub Desktop.
ReorderableList with Foldout for simplified setting
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
/// Copyright (c) 2019 hananoki | |
/// This code is released under NYSL Version 0.9982 | |
/// See http://www.kmonos.net/nysl/NYSL.TXT | |
using UnityEngine; | |
#if UNITY_EDITOR | |
using UnityEditor; | |
using UnityEditorInternal; | |
using System.Reflection; | |
public class SimpleReorderableList { | |
ReorderableList m_lst; | |
SerializedObject m_serializedObject; | |
SerializedProperty m_serializedProperty; | |
public static SimpleReorderableList CreateInstance( SerializedObject serializedObject, string propertyName ) { | |
var s = new SimpleReorderableList(); | |
s.Create( serializedObject, propertyName ); | |
return s; | |
} | |
public void Create( SerializedObject serializedObject, string propertyName ) { | |
m_serializedObject = serializedObject; | |
m_serializedProperty = serializedObject.FindProperty( propertyName ); | |
m_lst = new ReorderableList( serializedObject, serializedObject.FindProperty( propertyName ) ); | |
m_lst.drawElementCallback = ( rect, index, isActive, isFocused ) => { | |
var rc1 = rect; | |
rc1.y += 1; | |
rc1.height = EditorGUIUtility.singleLineHeight; | |
EditorGUI.PropertyField( rc1, serializedObject.FindProperty( propertyName + ".Array.data[" + index + "]" ) ); | |
}; | |
m_lst.drawHeaderCallback = ( rect ) => { | |
var rc1 = rect; | |
rc1.x += 10; | |
string title = string.Empty; | |
if( m_serializedProperty.isExpanded ) { | |
title = $"{m_serializedProperty.displayName}"; | |
} | |
else { | |
title = $"{m_serializedProperty.displayName} [{m_serializedProperty.arraySize}]"; | |
} | |
m_serializedProperty.isExpanded = EditorGUI.Foldout( rc1, m_serializedProperty.isExpanded, title ); | |
}; | |
m_lst.elementHeight = EditorGUIUtility.singleLineHeight + 3; | |
} | |
public void DoLayoutList() { | |
if( m_serializedProperty.isExpanded ) { | |
m_lst.DoLayoutList(); | |
} | |
else { | |
m_lst.DoListHeader(); | |
} | |
} | |
} | |
public static class ReorderableListExtension { | |
static FieldInfo _s_Defaults; | |
static MethodInfo _DoListHeader; | |
public static void DoListHeader( this ReorderableList r ) { | |
if( _s_Defaults == null ) { | |
_s_Defaults = typeof( ReorderableList ).GetField( "s_Defaults", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic ); | |
} | |
if( _DoListHeader == null ) { | |
_DoListHeader = typeof( ReorderableList ).GetMethod( "DoListHeader", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic ); | |
} | |
if( _s_Defaults.GetValue( null ) == null ) { | |
_s_Defaults.SetValue( null, new ReorderableList.Defaults() ); | |
} | |
var rect = GUILayoutUtility.GetRect( 0f, 18f, GUILayout.ExpandWidth( true ) ); | |
_DoListHeader.Invoke( r, new object[] { rect } ); | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment