Game Development Reference
In-Depth Information
Player Preferences Class
Perhaps the easiest and quickest way to save persistent data with Unity on all platforms is to use
the
PlayerPrefs
class. This class abstracts itself from the local file system so that we don't have
to worry about file names or specific file system paths. Instead, we can treat it like a key-value
database. We write values to the database using integer, float, and string values, and data such as
the game's brightness, whether the game should run in full-screen mode, the name of the gamer,
and more. And we then read back those values, across playing sessions, by simply querying the
appropriately named keys. As we do this, Unity internally handles all specifics about how data
is written to and read from persistent storage. More information on
PlayerPrefs
can be found in
the online Unity documentation at
https://docs.unity3d.com/Documentation/ScriptReference/
PlayerPrefs.html
. Consider Listing 9-1, where some sample data is written to persistent storage
with the
PlayerPrefs
class.
Listing 9-1. Saving Data with the PlayerPrefs Class
PlayerPrefs.SetString("PlayerName", "John Smith");
PlayerPrefs.SetInt("LastLevel", 10);
PlayerPrefs.SetFloat("Brightness", 0.7f);
PlayerPrefs.Save();
PlayerPrefs
is a static class, meaning it can be accessed anywhere in a
C# script file; its scope is global.
The
PlayerPrefs
class automatically saves and commits all changes on a clean
application exit, and so in theory, the final call to the
Save
method is optional.
However, the
Save
function will not be called automatically if the application
terminates prematurely, such as an unexpected crash. So it's often good
practice to call the
Save
method after making
PlayerPrefs
changes.
Saved data can also be retrieved through the
PlayerPrefs
class at any time across all playing
sessions using the
GetString
,
GetInt
, and
GetFloat
methods (see Listing 9-2).
The
Listing 9-2. Loading Data with the PlayerPrefs Class
string Name = PlayerPrefs.GetString("PlayerName");
int LastLevel = PlayerPrefs.SetInt("LastLevel");
float Brightness = PlayerPrefs.SetFloat("Brightness");
Saving and loading data with
PlayerPrefs
really is as simple as Listings 9-1 and 9-2 show.
From this, the question may arise as to exactly where on the local file system the data is stored.
The answer is: it depends on the user operating system. For Windows users,
PlayerPrefs
data is
stored in the system registry, and for other systems, it's stored in local files in different locations.
The Unity online documentation features more information on this. However, the
PlayerPrefs
class
will not be used further in this chapter for storing CMOD data. This is because
PlayerPrefs
is
intended for storing only user preferences data—that is, smaller, bite-sized pieces of data for holding
brightness settings, as well as the gamer's name, game difficulty, full-screen vs. windowed mode,
and more. For CMOD, we'll need to store a lot more data than this: specifically, the transform data
for nearly every moveable object, including the Player and the Enemies. To achieve this, we must
move away from
PlayerPrefs
and adopt a custom file-based solution.