Serialization of List<object> using ScripterableObject</object>

Hi there Uniteers. I am currently developing a hierarchy creation system in an editor window, and I have trouble serializing my newly created data, as it simple gets destroyed upon play/exit in the editor.

What the tool can do is create a hiearchy tree of custom classes:

  • Quest Manager
    • Quest
      • SubQuest
        • ObjectiveInSubQuest

Each of these classes inherit from a class called “HiearchyEntry”, in which is a List that contains all the objects of the “sub” class. So basically, QuestManager have a list of Quest. Each Quest have a list of SubQuests etc…
The HiearchyEntry contains information about the quantity of sub-types and index values while the child class have some custom logic for game purposes.

The scripts I use are:

Base class: - Contains List<object> where Object is the sub element in the hiearchy


[System.Serializable]
public class HiearchyEntry : ScriptableObject
{
    [SerializeField]
    List<Object> listOfEntries = new List<Object>();
}

###Child class: - Inherits from HiearchyEntry, and contain custom code for game logic


[System.Serializable]
public class QManager : HiearchyEntry
{
    public void GetAllActiveQuests()
    {
      // Insert game logic
    }
}

###Editor script: - Get the reference of QManager from Manager and feed the information to the editor window


[CustomEditor(typeof(Manager))]
public class QManagerEditor : Editor
{
    public override void OnInspectorGUI()
    {
        if (GUILayout.Button("Open Quest Manager"))
        {           
            Manager ManagerMonobehaviour = (Manager)target;

            if (ManagerMonobehaviour.qmanager == null) // Creates a new instance of QManager if it doesn't exist
                ManagerMonobehaviour.qmanager = (QManager)ScriptableObject.CreateInstance(typeof(QManager));
            
            QManagerWindow newlyCreatedWindow = (QManagerWindow)EditorWindow.GetWindow(typeof(QManagerWindow)); // Opens the editor window
            newlyCreatedWindow.InitializeWindow(ManagerMonobehaviour.qmanager); // Initiliaze the new window with the QManager.
        }
    }
}

The creation of the hiearchy is functioning as intended, but whenever I enter/exit playmode, all my hiearchy data is lost and you will have to create the hiearchy from scratch.

I have looked through various posts and blogs about serialization etc, but nothing have worked out, I am not even sure what I am trying to do is possible.

I have alot of code, and so I have tried to minimize it to, what I believe, is the error. Pleas ask for more information if you think I have left something out.

Unity’s serialization system doesn’t support inheritance for custom classes. We had that kind of questions quite a few times.

If you need inheritance you have those options:

  • Use MonoBehaviour as base class. That means all your instances are components and have to be created with AddComponent and therefore are attached to some GameObject / prefab.
  • Use ScriptableObject as base class. Those don’t need to be attached to GameObjects, but need to be created with ScriptableObject.CreateInstance() and they have to be serialized on their own. So you need to use the AssetDatabase to either create seperate asset files or add the asset to an existing asset. That of course only works in the editor, like the whole serialization system.
  • Use your own classes but don’t use Unity’s serialization system. So you have to serialize / deserialize your stuff on your own (JSON / XML / binary / …)

ps: All ScriptableObjects have the same requirement as MonoBehaviour classes, they need to be placed in a seperate file, named like the class.

You can find plugin here (easy to implement )