Game Development Reference
In-Depth Information
Classified intel
In Unity C#, we used
[System.Serializable]
to enable the ability to serialize this
object.
Serialization
is the process of translating the data structure or object state in the
format that can easily be stored (for example, a file/memory buffer, or transmitted data
across a network connection link) and reconstructed later in the same or another computer
environment.
Note
More explanation about serialization can be found from the following links:
Whether you add
@script System.Serializable
in the class or not, we will be
able to serialize them.
The serialization technique can be stored as a file and saved on your local machine too.
This is a very efficient way to save the game data, such as a player's hit point, player's loca-
tion, and enemies' hit point. To store the data to the file, we can add something similar to
the following code:
// Unity JavaScript user:
function SaveData () {
var binary : BinaryFormatter = new BinaryFormatter();
var file : FileStream = new
FileStream(Application.persistentDataPath+"/highscores.dat",
FileMode.Append);
binary.Serialize(file, _users);
}
function LoadData () {
try {
var binary : BinaryFormatter = new BinaryFormatter();
var file : FileStream = new
FileStream(Application.persistentDataPath+"/
highscoresJS.dat", FileMode.Open);