How to properly use ReorderableList inside CustomPropertyDrawer

I want to show a ReorderableList for every item of an array using a CustomPropertyDrawer.
Here’s my model:

Library.cs

[Serializable]
[CreateAssetMenu(fileName = "myLibrary", menuName = "Library", order = 0)]
public class Library : ScriptableObject
{
    public string title;
    public List<Book> books = new List<Book>();
}

[Serializable]
public class Book
{
    public string title;
    public List<Chapter> chapters;
}

[Serializable]
public class Chapter
{
    public string title;
}

As you can see it’s a simple library with a list of books, and every book has a list of chapters.
I will use a CustomEditor for the library and a CustomPropertyDrawer for each book. I would like to use a ReorderableList for the chapters of every book.

LibraryEditor.cs

[CanEditMultipleObjects]
[CustomEditor(typeof(Library))]
public class LibraryEditor : Editor
{
    private ReorderableList bookList;

    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        EditorGUILayout.PropertyField(serializedObject.FindProperty("title"));
        EditorGUILayout.PropertyField(serializedObject.FindProperty("books"), true);
        serializedObject.ApplyModifiedProperties();
    }
}

BookDrawer.cs

[CustomPropertyDrawer(typeof(Book))]
public class BookDrawer : PropertyDrawer
{
    ReorderableList chapterList;

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        float height = 0;
        EditorGUI.BeginProperty(position, label, property);
        {
            Rect titleRect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
            EditorGUI.PropertyField(titleRect, property.FindPropertyRelative("title"));
            height += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;

            Rect chaptersRect = new Rect(position.x, position.y + height, position.width, EditorGUIUtility.singleLineHeight);
            if(chapterList == null)
            {
                chapterList = BuildChaptersReorderableList(property.FindPropertyRelative("chapters"));
            }
            chapterList.DoList(chaptersRect);
        }
        EditorGUI.EndProperty();
    }

    private ReorderableList BuildChaptersReorderableList(SerializedProperty property)
    {
        ReorderableList list = new ReorderableList(property.serializedObject, property, true, true, true, true);

        list.drawHeaderCallback = (Rect rect) =>
        {
            EditorGUI.LabelField(rect, "Chapters");
        };
        list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => {
            EditorGUI.PropertyField(rect, property.GetArrayElementAtIndex(index), true);
        };
        return list;
    }
}

Here’s the final result:
112884-unity-drawer.png
As you can see the problem is that when I add/remove/edit a chapter on a book, all the other books update with the same data. I think this is because Unity uses the same PropertyDrawer instance for every Book in the array.

Does someone knows how can I avoid this?

How can I tell Unity to use a new BookDrawer instance for every book element in the array?

Thank you.

Your assumption is correct. Unity uses the same PropertyDrawer instance for elements in a list or array. You can see my answers here and here to related questions, but the idea is that any view data you need to cache should be stored per property path (i.e. in a string-keyed dictionary field).

Maybe this is just what you need?