- Home /
Saving/Loading ScriptableObject during runtime
According to the live training video, ScritableObjects are serialized using Unity's serilization techniques, and are better for saving dynamic lists etc (c# serialization cant do them). but all the tutorials online are talking about editor scripting! :(
I know I need to use CreateInstance to constuct my object, but if I use the technique from the live training video linked above, will the ScritableObject still use Unity's serialization? Save and load code is below, once saved, I'd presume 'CreateInstance' is not used to deserialize,
string fName = Application.persistentDataPath + "/" + FileName;
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(fName);
bf.Serialize(file, Data);
FileStream file = File.Open(fName, FileMode.Open);
object DataFromFile = bf.Deserialize(file);
I'd like each 'level' to have an 'Add object' feature, so whenever I'm saving I will have a list of unknown length to save to file. I could probably have some '.ToArray()' (linq perhaps?) method to avoid dynamic list, but I've heard good things about ScritableObjects
please be aware code above only has the 'vital' parts shown, FileStream
needs to be properly closed and disposed for one, try catch'es should be used where appropriate as well.
Answer by ninjanoel · Feb 08, 2015 at 04:03 PM
Other similar questions, with good answers...
Can you use a ScriptableObject to save game state?
Save game using scriptable object derived custom asset
So basically, ScritableObjects are for Editor use only, like a "configuration file" of data that can be treated like an asset (*.asset, can be source-controlled etc) while in the Unity Editor, and can be used in script similar to GameObject, always created with CreateInstance(), supporting DontDestroyOnLoad()
C# native serilization does not play well with UnityEngine.Object's in general, so using the BinaryFormatter
as specified in the question, with a "plain" class (class not extending UnityEngine.Object), is a good option.
Apologies for answering my own question, changed my search term and actually found what I needed
Answer by Firemaw · Jun 11, 2018 at 03:19 PM
In Unity 5.3+, the JsonUtility can convert any MonoBehaviour, ScriptableObject, or plain class/struct with the Serializable attribute to a JSON string.
https://docs.unity3d.com/ScriptReference/JsonUtility.html
You can create an instance of a ScriptableObject at runtime using ScriptableObject.CreateInstance and edit its properties via script. You can use JsonUtility to convert the ScriptableObject instance to a JSON string, which can be saved and loaded from disk using PlayerPrefs. After retrieving a JSON string from disk, the values can be copied over an instance of a ScriptableObject using JsonUtility.FromJsonOverwrite.
Just marking a class with Serializable attribute does not guarantee that it is indeed serializable. JsonUtility will fail to serialize classes which are not entirely serializable, in the same way as BinaryFormatter.
Your answer
![](https://koobas.hobune.stream/wayback/20220613182801im_/https://answers.unity.com/themes/thub/images/avi.jpg)