Picture it: you are in the middle of creating the game of your dreams. Everything seems to be going well, but you can’t for the life of you figure out how to have players save their game progress. If you are anything like us, you compulsively save after every significant game event, (and sometimes create multiple files…just in case).
It doesn’t exactly help that games these days can quickly run from 50 to 100 hours (I’m giving an especially harsh side-eye to you, Persona 5 Royal). Games aren’t these novel experiences where you could wrap it up in a night. They are full-fledged epics, with wide-sweeping story arcs, character development, and massive worlds.
How do you even begin to tackle the problem of saving in your game? It’s an absolute must, but how does this translate to making a Unity save game? When making your game, how do you want your players to save their progress? Do you want it to be manual saves, regardless of where they are in the game’s level?
Let’s take a deeper dive into Unity save games and what you can do to better the players’ experiences.
The Different Ways to Save Data in Unity:
Unity is one of the most popular game engines in video gaming, so it may come as no surprise that its save options are just as varied as all of its other features. Before we get into the nitty-gritty, we need to familiarize ourselves with the foundations of Unity save data.
Fundamental Unity Save Game Concepts
Consider Unity PlayerPrefs to be ‘the easy way,’ of sorts. It’s a fundamental cache system that keeps track of important things in game development.
It’s by no means the be-all-end-all, as it’s better suited for remembering critical aspects of the game, rather than where your player last left off. It will recognize that English subtitles are activated rather than what level they’re in.
This is a valuable building block in the Unity save location process.
Some Static Methods with Unity PlayerPrefs are commands like:
- DeleteAll: Removes all keys and values from the preferences. Use with caution.
- DeleteKey: Removes key and its corresponding value from the preferences.
- GetFloat: Returns the value corresponding to the key in the preference file if it exists.
- GetInt: Returns the value corresponding to the key in the preference file if it exists.
- GetString: Returns the value corresponding to the key in the preference file if it exists.
- HasKey: Returns true if the key exists in the preferences.
So serialization should be considered ‘the hard way,’ compared to its predecessor, Playprefs. Everything after this makes PlayPrefs look like amateur hour.
What Is Serialization?
Very basically, serialization is the process in which objects are effectively translated into bytes. This is when Unity takes those scripts that you wrote over the hours and hours of making your game and turns them into things like RAM, files, and more.
How Does Serialization Tie in with Unity Save File Locations?
Disregarding Unity serialization for a moment, plain old serialization of data is how computers can retrieve advanced files and knowledge from formats that can be reconstructed later.
Sound familiar? Sounds an awful lot like save games to me.
If you see the term ‘deserialization’ anywhere, it just means the opposite of serialization. Those bytes and pieces of information are turned into objects. Simple, right?
What Is JSON, and How Would I Use It?
This is easily one of the most useful features regarding Unity save file locations, as it transcends different programming languages and platforms.
You enter a string of data, and in the newest forms of Unity, you can save data strings as JSON right away. This streamlines the process quite a bit.
After you open your JSON data string in a new program or want to translate it to a different coding language, type in this command:
JSON is a fantastic method to engage in the cloud saving of your game.
Overwriting Objects with JSON:
If you want to replace certain aspects of your Unity save data, using JSON to overwrite features can work. This is where what we learned regarding serialization comes in handy. JSON uses serialization, so you need to have serialization down as a concept if you want to progress further in the JSON overwrite.
Here is the code provided by Unity themselves:
What Works With JSON?
For successful use with JSON, your script needs to be the following:
- A string.
- A number.
- an object (JSON object)
- An array.
- A boolean.
The performance levels with JSON are quite good. They are preferred over many other types, and it consumes minimal memory if that’s a concern.
The three main things that consume the most memory are:
What happens when JSON is confronted with unknown types of objects and files?
SO what happens when the JSON type isn’t recognized? Or what type of object it is? This is where our knowledge of deserialization comes in to save the day. You have to deserialize JSON files into a ‘common’ filed, deconstructing the fields as you go. You then need to double down on serialization. Breaking down the complicated bits can help you tremendously with your Unity save file location.
What Is A PlayerPrefs Is?
Its the stored data cache in which player preferences and primary data is stored for future use. This is perfect for games that require no deep story or player progression. This is geared towards more fast-paced and low stakes games.
Using Unity PlayerPrefs isn’t a good move if you plan to build your game bigger.
How Do I Use It to Save Player Settings?
To save player settings through using PlayerPrefs, you need to type in a wall of code:
Saving Data with JSON:
As already stated, you can now save files directly as JSON files. This makes things quite a bit smoother in the Unity save game process.
Where Are the Unity Save Files Located?
While there is no universal save file location, the unofficial locations for Unity save games are all located by default in ‘persistantDataPath,’ in the application.
For example, Windows users will be familiar with saving things to your C disk on your hard drive. Therefore, you would find your save files in a format something like this: ‘C:Users[yourname]AppDataLocalLow[project]’
If you are only using Unity PlayerPrefs, then the data will simply be in the registry.
Best Location to Save Game Data:
Why complicate a good thing? The best places to contain your Unity save data is right where they save by default, the persistantDataPath.
Now that you know a little more about the details of Unity save data, it is time to get down to business and figure out which save system works best for you. If you are developing a rather simple game, based around quick, short rounds, then Unity Playerprefs may be your answer.
However, if you have a Unity game like Ori and the Blind Forest or Shadow Tactics: Blades of the Shogun, you are going to need to do some heavy lifting. The last thing you want is for people to pour in hours of dedication, hard work, and fun only to have the file corrupt or merely save the preferences.
Whichever you choose, read more about it in the official Unity documentation, and let us know what works best for your game!