- Home /
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!
Answer by Ran-Quan · Mar 21, 2017 at 06:07 AM
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
}
Note that the only cases where Unity does support polymorphism is for classes derived from UnityEngine.Object. The only classes you can actually use as base classes are "ScriptableObject" and "$$anonymous$$onoBehaviour". You can't derive a class directly from UnityEngine.Object and most other classes which are derived from UnityEngine.Object are sealed.
To better understand how Unity serializes "normal" serializable classes i suggest to set the asset serialization mode to "Force Text" and open your asset in a text editor. You will see that those custom serializable classes aren't serialized as "objects" but simply as nested properties of your ScriptableObject. No type information is stored. That means when deserializing your custom classes, Unity only relies on the variable type. The deserializer would even create an instance of an abstract class ^^.
That's a great workaround, feels obvious but once you get it in your head that you want to go with OOP it's hard to think outside that box sometimes. Worked a treat, thanks a lot.
Answer by $$anonymous$$ · Jan 03 at 07:28 PM
It's possible natively since 2019.3 release via [SerializeReference] attribute https://docs.unity3d.com/ScriptReference/SerializeReference.html