- Home /
ScriptableObject : Serialized Polymorphism Classes Can Not be Deserialize with Polymorphism
I make ScriptableObject to use local Database. It includes lists of "Skill". Skills are inherited from SkillBase class. And effect of Skill is written in override method.
Then, I added inherited Skill classes to List , and deserialize it in Game. However, it's effect does not act.
I guess ScriptableObject cannot deserialize with Polymorphism. How shuold I do.
DataTable.cs
public class DataTable : ScriptableObject
{
public List<SkillBase> SkillTable = new List<SkillBase> ();
}
SkillBase.cs
[System.Serializable]
public class SkillBase
{
public virtual void SkillExecute ()
{
}
}
[System.Serializable]
public class Skill1 : SkillBase
{
public override void SkillExecute ()
{
}
}
[System.Serializable]
public class Skill2 : SkillBase
{
public override void SkillExecute ()
{
}
}
I'm also interested in this question, but I fear it is as you suspect, serialization and polymorphism don't work together.
An alternative approach is to serialize the skill class type (e.g. an enum), and instantiate based on that value, but it is far from optimal and requires lot of hard-coding.
Answer by Xarbrough · Mar 09, 2018 at 10:22 AM
Do this:
public class SkillBase : ScriptableObject
Then create instances of skills in your project via the CreateAssetMenuAttribute. Now polymorphism works.
Your answer
Follow this Question
Related Questions
Why doesn't my ScriptableObject save using a custom EditorWindow? 3 Answers
How to make a deep copy of a list of ScriptableObject derived types? 1 Answer
[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer
pass child class to ScriptableObject.CreateInstance<> ? 1 Answer