Skip to content

Instantly share code, notes, and snippets.

@Democide
Last active November 15, 2022 14:01
Show Gist options
  • Save Democide/70da781eb2706899b823de6af42d0871 to your computer and use it in GitHub Desktop.
Save Democide/70da781eb2706899b823de6af42d0871 to your computer and use it in GitHub Desktop.
A simple helper class that generates a Reorderable List in Unity that can be folded out.
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
public static class ReorderableListUtility {
public static ReorderableList GetListWithFoldout(SerializedObject serializedObject, SerializedProperty property, bool draggable, bool displayHeader, bool displayAddButton, bool displayRemoveButton) {
var list = new ReorderableList(serializedObject, property, draggable, displayHeader, displayAddButton, displayRemoveButton);
list.drawHeaderCallback = (Rect rect) => {
var newRect = new Rect(rect.x + 10, rect.y, rect.width-10, rect.height);
property.isExpanded = EditorGUI.Foldout(newRect, property.isExpanded, property.displayName);
};
list.drawElementCallback =
(Rect rect, int index, bool isActive, bool isFocused) => {
if (!property.isExpanded) {
GUI.enabled = index == list.count;
return;
}
var element = list.serializedProperty.GetArrayElementAtIndex(index);
rect.y += 2;
EditorGUI.ObjectField( new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element, GUIContent.none);
};
list.elementHeightCallback = (int indexer) => {
if (!property.isExpanded)
return 0;
else
return list.elementHeight;
};
return list;
}
}
@Democide
Copy link
Author

Democide commented Apr 14, 2017

Used like so in a custom Editor:

[CustomEditor(typeof(InspectMe), true)]
public class InspectMeEditor : Editor {
	private ReorderableList list;

	void OnEnable() {
		var stringArrayProperty = serializedObject.FindProperty("stringArrayName");
		list = ReorderableListExtensions.GetListWithFoldout(serializedObject, stringArrayProperty, true, true, true, true);
	}

	public override void OnInspectorGUI() {
		serializedObject.Update();
		list.DoLayoutList();
		serializedObject.ApplyModifiedProperties();
    }

and for reference here's that fictional class that's being inspected:

public class InspectMe: ScriptableObject {
    public string[] stringArrayName;
}

@SolidAlloy
Copy link

In Unity 2019, the foldout doesn't work properly anymore because ReorderableList caches the element height. It has the ClearCache() method, but it is internal, so it must be accessed through reflection. I use it like this:

  1. Get MethodInfo of ClearCache and store it in a static variable:
private static readonly MethodInfo _clearCacheMethod = typeof(ReorderableList)
    .GetMethod("ClearCache", BindingFlags.Instance | BindingFlags.NonPublic);
  1. Store ClearCache delegate action and the list itself in instance variables:
private ReorderableList _list;
private Action _clearCache;
  1. Create a delegate for the ClearCache method after the list is created:
_list = new ReorderableList(...);
_clearCache = (Action) Delegate.CreateDelegate(typeof(Action), _list, _clearCacheMethod);
  1. Use the delegate inside drawHeaderCallback:
bool newValue = EditorGUI.Foldout(newRect, property.isExpanded, property.displayName);

if (property.isExpanded == newValue)
    return;

property.isExpanded = newValue;
_clearCache();
  1. I then expose methods that I want to use for the foldout list:
public void DoLayoutList()
{
    _list.DoLayoutList();
}

@WolvenBard
Copy link

In Unity 2019, the foldout doesn't work properly anymore because ReorderableList caches the element height. It has the ClearCache() method, but it is internal, so it must be accessed through reflection. I use it like this:

  1. Get MethodInfo of ClearCache and store it in a static variable:
private static readonly MethodInfo _clearCacheMethod = typeof(ReorderableList)
    .GetMethod("ClearCache", BindingFlags.Instance | BindingFlags.NonPublic);
  1. Store ClearCache delegate action and the list itself in instance variables:
private ReorderableList _list;
private Action _clearCache;
  1. Create a delegate for the ClearCache method after the list is created:
_list = new ReorderableList(...);
_clearCache = (Action) Delegate.CreateDelegate(typeof(Action), _list, _clearCacheMethod);
  1. Use the delegate inside drawHeaderCallback:
bool newValue = EditorGUI.Foldout(newRect, property.isExpanded, property.displayName);

if (property.isExpanded == newValue)
    return;

property.isExpanded = newValue;
_clearCache();
  1. I then expose methods that I want to use for the foldout list:
public void DoLayoutList()
{
    _list.DoLayoutList();
}

This is brilliant! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment