- Home /
Save multiple scriptableObjects in just one binary file
Hi, I have been trying to save and load several scriptable objects in just one binary file but with no success.
Any Idea of how to make this?
Thanks for you time.
Alternatively you cold make a SaveData class which just stores your Save Data. The list that @Ganion_Player suggested would work fine, but you'd have to parse the data out, which with only 2 files is nearly insignificant.
Answer by $$anonymous$$ · Feb 20, 2017 at 07:56 PM
Have you tried to serialize a list of these objects? I haven't worked too much with saving data, but I think that should work. You would just need to make sure that index 0 is always referencing the Construction object and index 1 is always references the Hero object.
For example, inside the using statment in the save method you could probably do the following:
List<List<ScriptableObject>> objects = new List<List<ScriptableObject>>();
objects.Add(TownManager.instance.MyConstructions.Database);
objects.Add(HeroManager.instance.MyHeroDatabase.Database);
formatter.Serialize(saveFile, objects);
saveFile.Close();
and then in the using statment in the load method:
List<List<ScriptableObject>> objects =
List<List<ScriptableObject>>formatter.Deserialize(saveFile);
TownManager.instance.MyConstructions.Database = (List<Construction>) objects[0];
HeroManager.instance.MyHeroDatabase.Database = (List<Hero>) objects[1];
saveFile.Close();
I haven't actually tested this code, so I've probably made a mistake somewhere. Assuming I haven't and both the Construction and Hero class inherits from ScriptableObject, it should work.
I hope this helped!
THAN$$anonymous$$S!! You are a genius! I used your sugestion and I made some changes to get the solution that I was looking for.
You helped me a lot. $$anonymous$$any thanks and I wish you lots of success with you games.
Answer by Bunny83 · Feb 20, 2017 at 08:11 PM
I have never tried the BinaryFormatter with ScriptableObjects but based on how ScriptableObjects work it should not be possible to deserialize them. ScriptableObjects need to be created with the static CreateInstance method and must not be created with "new". Since the BinaryFormatter only works with plain C# classes it always uses the constructor of the serialized classes when you deserialize them.
The deserialization might even work, but the actual ScriptableObjects wouldn't be initialized correctly as their native C++ counter part would be missing and they would come up as "null". If you want to use .NET serialization you shouldn't derive your classes from ScriptableObject. If you need them to be ScriptableObjects you shouldn't use .NET serialization.
Hi there, I've seen a lot answers about ScriptableObject from you and I believe you're an expert about it, you mentioned the advantage of ScriptableObject vs Normal Class and so on which is very valuable. I get suggestion from others that I should use the SO as a storage of preset data at editor time and should not try to change the data held by a GameObject which reference to the SO asset because of an uncontrollable behavior (I post the question at https://forum.unity3d.com/threads/question-about-the-data-storage-of-scriptableobject-at-runtime.477091/#post-3111035). I wonder if I should put the data changed when playing to a System.Serializable class as a temporary container and then put them to the file using BinaryFormatter as a persistence? How about the SO, if I put data changed to the SO and how should I to put them in the file? Or if there is a more proper method to do this? Thanks previously : )
Your answer
Follow this Question
Related Questions
Save system for a Motherload-like game. 1 Answer
Writing binary files on Android 1 Answer
SerializationException: Type UnityEngine.UI.Button is not marked as Serializable. Solution 1 Answer
BinaryFormatter - saving and loading a list containing sprites. 0 Answers
Delete a serialized binary save file 2 Answers