- Home /
Question included mistakes not related to the question. Fixed.
How to serialize a concrete derived class from generic ScriptableObject base
I would like to use a generic ScriptableObject class as a base for multiple children, which then should be serialized. I thought this would work the same way it does with normal generic classes and concrete serializable child classes, but apparently it doesn't:
// Not serializable.
public class GenericClass<T>
{
public T Data;
}
// Serializable.
[System.Serializable]
public class ConcreteClass : GenericClass<int> { }
// Not serializable.
public class GenericSO<T> : ScriptableObject
{
public T Data;
}
// Should be serializable, but isnt. Why?
public class ConcreteSO : GenericSO<int> { }
Am I doing something wrong or is there a reason that ScriptableObject derived classes won't serialize although they are concrete implementations of the generic base? Is there a smart way around this? I basically I want the base class to hold a KeyValuePair-like data structure, for which the value is always of the same type, but the key is different. Subclassing this with normal classes makes sense and works fine, but I want instances as assets, so I need SO.
Thanks for any suggestions! :)
Answer by frarees · Jan 06, 2016 at 04:44 PM
Mark ConcreteSO
and GenericSO´1
class definitions with a System.Serializable
attribute as you did with ConcreteClass
. That should do for serializing ConcreteSO
fields.
Thanks for answering, but the Serializable attribute is not needed with ScriptableObject as far as I know. I've also tested and it doesn't make a difference. However, I just found out that my posted code works in a new project, so I must have been doing something else wrong. Closing the question.