Difference between serialization and saving data
No matter how much I read up on this I can't get it... I'm hoping to start implementing an automatic saving system into my game soon but want to clear this up as much as possible first.
The method I was leaning towards was json. But what is unity serialization and do I ever need to use it? If so what for? I've only used [System.Serializable] a few times to make things show in the Inspector and up until recently I thought that was the main use of it.
Answer by haruna9x · Aug 08, 2017 at 11:42 PM
Serialization is the automatic process of transforming data structures or object states into a format that Unity can store and reconstruct later.
Saving data is simply writing data to a file.
Simplicity of serialization makes archiving and data recovery an automatic and easier for the user.
You can still save your data manually, by reading and writing files as you normally would, but it is a bit difficult with a complicated data structure.
So what is actually happening when I put [System.serializable] above one of my classes? Isn't that needed to write data to a json file, as well as expose it in the Inspector?
$$anonymous$$aybe you could explain further with a simple example please? I'd really appreciate it. Not sure why I can't grasp this
Some of Unity's built-in features use serialization; Features such as saving and loading, the Inspector window, instantiation, and Prefabs
For classes not derived from $$anonymous$$onoBehaviour, you need to add [System.Serializable] so that Unity can serialize it. For classes derived from $$anonymous$$onoBehaviour, Unity will automatically do for you.
Some principles of serialization, you can refer to it here: https://docs.unity3d.com/$$anonymous$$anual/script-Serialization.html https://docs.unity3d.com/ScriptReference/SerializeField.html
Answer by Saryk360 · Aug 07, 2017 at 02:23 PM
Basically, saving a game is just writing the data to the hard drive. JSON is a format to do just that, and the process of turning your game data into a JSON string is called "serialization".
"Serialization" is the process by which you save your game if you use JSON.
And yes, you should definitely use it, it's easy and very handy.
So any data fields that I wish to write to json file must simply have [Serializable] before it? Or for a specific variable [serializefield]?