CustomEditor for a list of polymorphic items

Hi all, been struggling with this for a while so I hope someone can help.

To schedule cutscenes in my game I have a scriptableobject named Scene:

public class Scene: ScriptableObject
{
    public List<ActingDirection> directions = new List<ActingDirection>();
}

Scene has a list of ActingDirections:

public class ActingDirection {
    public int actor;   //Who is performing the action
    public float timeStart;   //The time after the previous action this one should play
}

And there are two classes that extend ActingDirection: ActingDialogue and ActingAnimation, which respectively contain a string of dialogue, or an enum specifying an animation.

I’ve managed to get a custom editor working for the Dialogues on their own:

list = new ReorderableList(serializedObject,
                serializedObject.FindProperty("directions"),
                true, true, true, true);

        list.drawElementCallback =
        (Rect rect, int index, bool isActive, bool isFocused) => {
            var element = list.serializedProperty.GetArrayElementAtIndex(index);
            rect.y += 2;
            EditorGUI.PropertyField(
                new Rect(rect.x, rect.y, 60, EditorGUIUtility.singleLineHeight),
                element.FindPropertyRelative("actor"), GUIContent.none);
            EditorGUI.PropertyField(
                new Rect(rect.x + 60, rect.y, 600, EditorGUIUtility.singleLineHeight),
                element.FindPropertyRelative("dialogue"), GUIContent.none);
            EditorGUI.PropertyField(
                new Rect(rect.x + rect.width - 55, rect.y, 40, EditorGUIUtility.singleLineHeight),
                element.FindPropertyRelative("timeStart"), GUIContent.none);
            EditorGUI.PropertyField(
                new Rect(rect.x + rect.width - 10, rect.y, 20, EditorGUIUtility.singleLineHeight)

That worked fine for just the dialogue objects but when I try to combine both the list shows up blank and complains about an object reference not set to an instance.

I’d ideally like something like the mockup below:

Is this possible? I don’t know enough about how serialization or editor scripting works to figure this one out on my own!

Unity’s serializer has no support for polymorphism. Read more

Maybe you should unify your multiple class definitions into one single format and draw different UI based on the data. Maybe something like:

if (directionType == ActingDirectionType.Dialogue) {
    // draw dialogue UI
} else if (directionType == ActingDirectionType.Animation) {
    // draw animation UI
}

It’s possible natively since 2019.3 release via [SerializeReference] attribute Unity - Scripting API: SerializeReference