Order of a serializable list not saved correctly using ReorderableList

I’m implementing a custom editor to rearrange in a visual way the elements of a list, where each element is a wrapper object for each value of an enum. To achieve this I use a ReorderableList so I can change the position of each element in the list just dragging and dropping. This works fine, the list is initialized correctly and i’m able to rearrange the elements. The code is as follows.

The enum:

public enum PlayerAction {
    PlayerAction1, PlayerAction2, PlayerAction3
}

The MonoBehaviour:

[Serializable]
public class PlayerActionPriority {
    public PlayerAction action;

    public PlayerActionPriority(PlayerAction action) {
        this.action = action;
    }

}

public class PlayerActionManager : MonoBehaviour {

    [HideInInspector]
    public List<PlayerActionPriority> actionPriorities;

}

The custom Editor:

[CustomEditor(typeof(PlayerActionManager))]
public class PlayerActionManagerEditor : Editor {

    private PlayerActionManager _myScript;    
    private ReorderableList _actionPriorityList;

    public void OnEnable() {
        _myScript = (ActionManager)target;

        // list initialization only if it's empty (the component just has been added)
        if (_myScript.actionPriorities.Count == 0) {
            PlayerAction[] values = (PlayerAction[]) Enum.GetValues(typeof(PlayerAction));

            foreach(PlayerAction value in values) {
                _myScript.actionPriorities.Add(new PlayerActionPriority(value));
            }
        }

        _actionPriorityList = new ReorderableList(
            _myScript.actionPriorities, typeof(PlayerActionPriority), true, true, false, false);

        _actionPriorityList.drawHeaderCallback = (Rect rect) => {
            GUI.Label(rect, "Action priority");
        };

    }
        
    public override void OnInspectorGUI() {
        DrawDefaultInspector();

        _actionPriorityList.DoLayoutList();
    }

}

The list elements are serialized correctly and sorted as specified while Unity is open… but the new order is lost after closing and reopening. It works if i modify the list directly (removing the HideInInspector) instead of doing it using the ReorderableList, so i think the problem is related to the custom editor.

Am i missing something?

I solved the problem following this article. The idea is to modify directly the serialized representation of the list instead of doing it through the object. These are the changes I made to the code:

MonoBehaviour

 [Serializable]
 public struct PlayerActionPriority {
     public PlayerAction action;
 }
 
 public class PlayerActionManager : MonoBehaviour {
     [HideInInspector]
     public List<PlayerActionPriority> actionPriorities = new List<PlayerActionPriority>();
 }

The custom Editor

[CustomEditor(typeof(PlayerActionManager))]
 public class PlayerActionManagerEditor : Editor {

     private ReorderableList _actionPriorityList;
 
     public void OnEnable() {
         _actionPriorityList = new ReorderableList(
             serializedObject, serializedObject.FindProperty("actionPriorities"), 
             true, true, false, false);

         Initialization();
 
         _actionPriorityList.drawHeaderCallback = (Rect rect) => {
             GUI.Label(rect, "Action priority");
         };

        _actionPriorityList.drawElementCallback = 
            (Rect rect, int index, bool isActive, bool isFocused) => {
            SerializedProperty element = 
                _actionPriorityList.serializedProperty.GetArrayElementAtIndex(index);
            PlayerAction action = 
                (PlayerAction)element.FindPropertyRelative("action").enumValueIndex;
            GUI.Label(rect, new GUIContent(action.ToString()));
        };
     }
         
     public override void OnInspectorGUI() {
         DrawDefaultInspector();
 
         _actionPriorityList.DoLayoutList();
     }

    void Initialization() {
        PlayerAction[] actions = (PlayerAction[]) Enum.GetValues(typeof(PlayerAction));

        foreach(PlayerAction action in actions) {
            if(!IsActionAlreadyAdded(action)) {
                AddActionItem(action);
            }
        }
    }

    bool IsActionAlreadyAdded(PlayerAction action) {
        for(int i = 0; i < _actionPriorityList.serializedProperty.arraySize; i++) {
            SerializedProperty element = 
                _actionPriorityList.serializedProperty.GetArrayElementAtIndex(i);
            if (element.FindPropertyRelative("action").enumValueIndex == (int) action) {
                return true;
            }
        }

        return false;
    }

    void AddActionItem(object obj) {
        int index = _actionPriorityList.serializedProperty.arraySize;
        _actionPriorityList.serializedProperty.arraySize++;
        _actionPriorityList.index = index;

        SerializedProperty element = 
             _actionPriorityList.serializedProperty.GetArrayElementAtIndex(index);
        element.FindPropertyRelative("action").enumValueIndex = (int)((PlayerAction)obj);

        serializedObject.ApplyModifiedProperties();
    }

 }

Everything else stays the same.

Now I have a fancy custom editor that allows me to sort easily the different elements of an enumerator.

Well

The list elements are serialized
correctly

It clearly isn’t serialized correctly if it isn’t saved :). You don’t seem to mark your object as dirty, so all your changes won’t be saved. If you would use the serialized object that the editor is providing it would handle this automatically when calling ApplyModifiedProperties. It also registers undo actions. The ReorderableList constructor has an overload that takes a serialized object as well as a serialized property.

When using “target” / “targets” manually you have to take care of marking the object as dirty. EditorUtility.SetDirty is kind of deprecated for scene objects as you can read on that page. I still recommend to read through it as it explains the actual problem. It also suggests to use Undo.RecordObject instead which will also provide an undo action for the editor.