1
\$\begingroup\$

I'm trying to setup a simple ReorderableList in Unity 2017.3 for a list of enums. Adding items workes fine but selecting, dragging and deleting items from the list does not (see gif).

enter image description here

Here is my code:

[Serializable]
public enum RoofType { HIP, GABLE, FLAT, NONE }


UnityEditorInternal.ReorderableList roofTypeList = new UnityEditorInternal.ReorderableList(roofTypes, typeof(RoofType), true, true, true, true);
roofTypeList.drawHeaderCallback = (Rect rect) =>
{
   EditorGUI.LabelField(rect, "Roof types");
};
roofTypeList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
{
   roofTypes[index] = (RoofType)EditorGUI.EnumPopup(rect, roofTypes[index]);
};
roofTypeList.DoLayoutList();

Any ideas why this does not work? My guess it is because I use enums and not classes but it is not documented anywhere.

\$\endgroup\$
  • \$\begingroup\$ Is all of the code in your snippet within OnInspectorGUI()? \$\endgroup\$ – talrnu Jun 25 '18 at 0:47
1
\$\begingroup\$

It looks like you may be re-creating the ReorderableList on each execution of OnInspectorGUI(). Unlike other types of inspector UI elements, this one should be instantiated elsewhere, and a reference to that instance should be used in OnInspectorGUI() instead. For example (based on this article):

public class MyListEditor : Editor
{
    private ReorderableList list;
    private void OnEnable()
    {
        list = new UnityEditorInternal.ReorderableList(roofTypes, typeof(RoofType), true, true, true, true);
    }
    public override void OnInspectorGUI()
    {
        list.DoLayoutList();
    }
}

I'll also note that the above linked article recommends using the SerializedObject version of the ReorderableList() constructor. Give that a try if you're still having trouble.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.