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);
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
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
In Unity 5.3+, the JsonUtility can convert any MonoBehaviour, ScriptableObject, or plain class/struct with the Serializable attribute to a JSON string.
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.