Skip to content

Instantly share code, notes, and snippets.

@Democide
Last active November 15, 2022 14:01
Show Gist options
  • Select an option

  • Save Democide/70da781eb2706899b823de6af42d0871 to your computer and use it in GitHub Desktop.

Select an option

Save Democide/70da781eb2706899b823de6af42d0871 to your computer and use it in GitHub Desktop.

Revisions

  1. Democide revised this gist Apr 14, 2017. No changes.
  2. Democide created this gist Apr 14, 2017.
    34 changes: 34 additions & 0 deletions ReorderableListUtility.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    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;
    }
    }