Saving serializable class list at runtime.

Good Day!

Status quo:
Currently, I use serializable classes for things like Items or characters in my game. These classes contain “stats” (ints, strings, floats, bools) but also Unity-classes (sprites, gameObjects) that define their appearance and are stored in lists (say databases), where they can be found by an integer ID if needed. All of it is used to generate procedural content. All of it works in runtime.

Issue:
I need to be able to save the lists at runtime. Either for me to use it (for debugging, content creation) in the editor or for the player, who expands the list of his known randomly created entities with every second he is playing.

What is the best way to achieve that?

I tried to use XML parsing but it doesn’t seem to work. Maybe because my classes contain unity-classes?
Also, it would make cheating very easy, if the player could just open the .xml in an editor and manipulate all values.

Thank you!

What I did:

I rewrote parts of my serializable container-classes to only contain serializable fields. For example, I changed the type “GameObject” variables pointing to the prefabs to strings that contain the corresponding path (resource folder).

Then I used binary formatter to save and load the lists like so:

private List <MyEntity> myEntityDB; 
    
//Load
public void LoadDatabase(){
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Open(Application.persistentDataPath + "/myEntityDatabase.edb", FileMode.Open);
    MyEntityDB = (List <MyEntity>)bf.Deserialize(file);
    file.Close();
}
//Save
public void SaveDatabase(){
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Create(Application.persistentDataPath + "/myEntityDatabase.edb");
    bf.Serialize(file, myEntityDB);
    file.Close();
}

Quite simple, but I am still not sure if this is the best way. At least I can save and load my generated content now.

EDIT:
I decided to convert my class lists into json string arrays before serializing. This way I can change my classes (adding new fields for example) without running into serialization errors. I am now quite happy with the solution.