Hey folks,
I searched and searched but couldn’t find any solution yet. I have a custom EditorWindow script. Within this script, in Editor mode, I create a structure which is pretty much a linked list of nodes.
[Serializable]
public abstract class Node {
List<Node> g_Children;
...
}
[Serializable]
public class NodeA : Node {
// implementation of abstract methods and such
// some use of inherited protected members from base class
}
Some object, has a reference to this created List:
[Serializable]
public class SomeObject {
[SerializableField]
Node tree;
...
}
Within the EditorWindow, a Node is created, with children. This is set as the tree
member value of the SomeObject.
On play, the tree
within SomeObject
turns to be null, which clearly indicates the tree wasn’t serialized.
I was wondering what is the “Unity” work around for this. Making the Node
class non-abstract is not an option, for the main implementation of this class is within its subclasses. The main idea is that tree
is a polymorphic linked-list.
Any help will be greatly appreciated it. Thanks.
Update:
Ok, I changed the structure of the code a bit based on the ScriptableObject suggestion by @Bunny83
Holder class:
public class SomeClass : MonoBehaviour {
[SerializableField]
private Node tree;
...
}
[Serializable]
public abstract class Node : ScriptableObject {
[SerializableField]
List<Node> Children;
...
}
[Serializable]
public class NodeA : Node {
// implements abstract methods
}
I am creating the instances with ScriptableObject.CreateInstance(typeof(NodeA))
Here is a capture from my Inspector… NPCSequence is the subclass of NPCNode which is the scriptable object from my example:
Even after applying changes to my prefab, the ScriptableObject dissapears on play.
I am just following the examples here: Unity Serialization Blog
Update
Ok, I got it to persist on play mode. What I did:
- Use ScriptableObject for the base (abstract) Node class.
- Create each node in the tree with CreateInstance
- Save the Asset using AssetDatabase.CreateAsset with unique name
That’s it. Thanks.