Game Development Reference
In-Depth Information
Note When saving data to persistent storage, Unity often uses the Company Name and Product Name
values in the Project Settings to produce a unique storage location for your data. Before saving game states,
therefore, be sure to set the Company Name and Product Name to meaningful values from the Player Settings
dialog, which is accessible by choosing Edit Project Settings Player from the application menu
(see Figure 9-1 ).
Figure 9-1. Configuring Company Name and Product Name from the Player Settings dialog
File-Based Persistence
The second and most flexible method for storing persistent data in Unity is to resort to the Mono
Framework for saving data to a file on local storage. Using this method, two main approaches are
available: data can be saved to a binary proprietary file or to a human-readable text-based file like
XML. Both represent file-based forms of persistent storage. The difference rests in whether the
gamer can open the saved file and see and edit the contents. The traditional approach in game
development is to use a proprietary file, primarily to prevent the user from breaking the game by
changing the data—intentionally or accidentally—to invalid values. But it's also to prevent cheating,
too, which can happen when gamers tweak the data (intentionally giving themselves extra lives, or
more health, or more items).
Unity allows saving to this kind of format by using the BinaryFormatter class. But in this topic, we'll
actually choose the more open and human-readable XML route for creating saved games. There
are two main reasons for this: first, a human-readable XML file allows us to easily debug not only
save-game functionality, but the game in general. It lets us see the value of objects, consolidated
into a single XML file; and we can also change the values and see the changes reflected in-game.
And second, an open save-game file also permits other developers and third parties to easily write
extensions or plug-ins for the game, adding their own data without breaking the XML scheme
already in place. This means we can quickly establish a potential community of “modders” or
“modifiers.” So, for this chapter, we'll avoid the binary format route, and choose the XML-based
saved-game file. When creating your own games, you'll need to make judgments about which
 
Search WWH ::




Custom Search