Persistent data and saved games
Allowing the gamer to save and load the state of their game is important for many games, especially longer duration games such as adventures, real-time strategies, and RPGs. In these cases, the game should allow the user to save and load game data to and from external files.
This is achieved in Unity using data serialization via either XML or binary files:
Serialization is the process of converting, or translating, data in memory (such as the state of a component on a GameObject
) into a stream that can be written to a file and then loaded back from a file to recreate the component in memory, as it was when it was saved. The process of creating a save-game, therefore, is about first deciding which data must be saved and loaded (which is game specific) and then creating a new class to hold that data. Consider the following code sample 10-4 (ObjSerializer.cs
) that can be attached to any GameObject
to serialize...