To try it out in-game, save your code and return to the Unity editor. Otherwise, it will print an error to the Unity console. So long as PlayerPrefs has one of those, it’s safe to assume it will have the remaining data. The code sample below uses HasKey to search for one of the keys declared in the SaveGame method, those keys being SavedInteger, SavedFloat, and SavedString. In other words, you’re checking that there’s any save data to be found. A good practice is to make sure that the PlayerPrefs for your game has at least one of the keys you’re looking for. You set your variables of choice to whatever is in PlayerPrefs, and you’re good to go. Loading data is essentially saving data done in reverse. On Mac OS, according to the Unity documentation, the PlayerPrefs is found at ~/Library/Preferences folder, in a file named ist.įigure 3: PlayerPrefs variables in the Windows Registry In an exe they can be found at HKEY_CURRENT_USER\Software\\. Bear in mind this is the location for when the game was run from the editor. On Windows, PlayerPrefs can be found in the Registry under HKEY_CURRENT_USER\Software\Unity\UnityEditor\\ (Figure 3), where company name and project name are names set up in the project settings. You may be wondering where this save data is on the computer. A message is also printed to Unity’s debug console as a little note to the developer saying the save was successful. Once PlayerPrefs is given all its information, Save is called and, as you may have guessed, saves the data. As seen above, three variables were set, all of them given a name, or key, followed by the variable to save. First, you must set some variables for PlayerPrefs to save. So, what’s happening? Well, as promised, PlayerPrefs saves the player’s game. The actual act of saving your game takes only a few lines of code. Next, some variables to hold the values to save will be needed. To begin, the Start and Update methods can be commented out or deleted as they will not be used to demonstrate the save functionality. Double-click the script to open Visual Studio. To see what saving a game entails, start with the SavePrefs script. Call these scripts SavePrefs and SaveSerial.
You create scripts by right-clicking in the Assets window and selecting Create->C# Script as shown in Figure 2. Once you create the project, you’ll need two scripts. For this project, you will want to use the 2D template for the easiest setup as shown in Figure 1. You just need a project and a couple of scripts. There’s minimal setup involved in testing these out. Both methods have their pros and cons, which will be covered as the practices are explored. On the other hand, the not so easy way involves serializing data and writing to a file for later use. Give a value to a key, call Save, and you’re done. They can be quickly described as “the easy way” and “the not so easy way.” The easy way involves Unity’s built-in PlayerPrefs system. Unity provides two ways to save a game’s data. Only when a game is short enough that it can be completed in a single session does the need for saving data diminish, but even in those scenarios, the feature can be very handy. Many games require multiple play sessions to see to the end, and it’s now expected that developers include ways to save the game and come back later.
The need for saving all kinds of data, ranging from how much progress the player made in-game or their character’s statistics, became universal whether the game be a simple, linear adventure from beginning to end or it consists of a huge open world. But it didn’t take long for technology to improve, and thus games got longer and more complex. At best, a game would save a high score and leave it at that. Saving Game Data with Unity - Simple Talk Skip to contentīack in the day, many games had little need for saving data since you could finish a game in about an hour.