- Home /
objects in array/list are cast back to base class on editor activity
so i have a Scriptable object containing different kind of nodes in a list, like so:
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "Data", menuName = "ScriptableObjects/ExampleSO", order = 1)]
public class ExampleSO : ScriptableObject
{
public List<Node> nodes;
[System.Serializable]
public class Node
{
public int id;
}
[System.Serializable]
public class ValueNode : Node
{
public float value;
}
}
and a custom editor like so:
using System;
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEditorInternal;
using System.Collections.Generic;
public class ExampleEditorWindow : EditorWindow
{
private ExampleSO mExampleSO = null;
[MenuItem("Window/ExampleSOEditor")]
public static ExampleEditorWindow OpenWindow()
{
return EditorWindow.GetWindow<ExampleEditorWindow>("ExampleSO Editor");
}
[UnityEditor.Callbacks.OnOpenAsset(1)]
public static bool OnOpenDatabase(int instanceID, int line)
{
ExampleSO generator = EditorUtility.InstanceIDToObject(instanceID) as ExampleSO;
if(generator != null)
{
ExampleEditorWindow generatorWindow = OpenWindow();
generatorWindow.mExampleSO = generator;
return true;
}
return false;
}
private void OnGUI()
{
foreach(var e in mExampleSO.nodes)
{
EditorGUILayout.LabelField((e is ExampleSO.ValueNode)? "value node" : "normal node");
}
if(GUILayout.Button("add value node"))
{
mExampleSO.nodes.Add(new ExampleSO.ValueNode());
}
}
}
now, if i create an exampleSO (Project->RMB->create->ScriptableObject->ExampleSO) and double click it, to use with the custom editor and i fill it with ValueNodes, they all show correctly as value nodes. but, somehow, when i edit the list in the unity inspector (remove array element or duplicate array element), or recompile, all nodes in the node list suddenly are only 'normal nodes'.
how come this behaviour occurs and is there any way to avoid this, and maintain the derived class ValueNode in the List?
Answer by Bunny83 · Jan 12, 2020 at 09:40 PM
And once again...
Unity's serialization system does not support polymorphism for custom serializable classes. Those classes behave like structs. No type information is serialized. The type of such a field is always the field type. That might sound strange but it's just as it is. You may want to have a look at how the serialized data actually looks like (just open your scene / scriptable object asset in a text editor). In the YAML format you will notice that only the data is serialized and no type information is stored.
If you need polymorphism you have to use ScriptableObjects or MonoBehaviour instances. These do not behave like structs. They are not serialized inline like custom serializable structs. Fields of such types are actually serialized as a "serialized reference" to another asset.
Finally do not confuse casting with conversion. A cast can always be reverted. A conversion can not. When your object tree is serialized the serializer will serialize those custom classes based on the field type. The field type in your case is a List of "Node"s. Therefore all instances in your list will be treated like Nodes. Of course when you deserialize them you get a List with just Node instances.
thank you for the answer! that clears it up! valid point on the cast vs. conversion.
If you need polymorphism you have to use ScriptableObjects or $$anonymous$$onoBehaviour instances.
just to clarify: this would, in this case, mean one Scriptable Object per Node. there is no easier / more compact way to store an undefined amount(List) of polymorph classes in one ScriptableObject.
You can store as many ScriptableObjects in one asset file as you like. This is generally possible with Unity's asset file format. You can use AssetDatabase.AddObjectToAsset in an editor script to add an object to an existing asset. Finally Unity provides us with the long awaited AssetDatabase.Set$$anonymous$$ainObject which allows us to specify which object is the "main" asset and will be viewed as parent. Unity does the same thing with imported models. All the generated objects (mesh, skeleton, etc) are added as sub assets to the actual model asset. You are free to add all kinds of assets to any asset file. So this includes all Unity asset types as well as your own types.
If you don't want all those sub objects to be visible in the inspector and only have them referenced, you can use the hideFlags to control their visibility.
Your answer
Follow this Question
Related Questions
Get list of all "Action" classes 1 Answer
How do I associate my custom object for the serializedProperty objectReferenceValue 1 Answer
cannot change variables on scriptableobject asset in editor 1 Answer
Select custom object from a list via PopUp in custom editor for ScriptableObject 0 Answers
ProjectWindowUtil.CreateAsset causes a memory leak 0 Answers