How to Save and Load a Game in Unity

Learn how to save and load a game in Unity using PlayerPrefs, Serialization and JSON. Complete your user’s experience by allowing them to save their game. By Anthony Uccello.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Saving Data With JSON

There’s one more trick you can use when you want to save data — and that is JSON. You could create a local JSON representation of your game save, send it to a server, then get that JSON (as a String) to another device and convert it from a string back to JSON. This tutorial won’t cover sending/receiving from the web, but it is very helpful to know how to use JSON — and it’s incredibly simple.

The format of JSON can be a little different than what you might be used from C# code, but it’s pretty straightforward. Here is a simple JSON example:

{
  "message":"hi",
  "age":22
  "items":
  [
    "Broadsword",
    "Bow"
  ]
}

The outer brackets represent the parent entity that is the JSON. If you are familiar with a Dictionary data structure, then JSON is similar. A JSON file is a mapping of key and value pairs. So the above example has 3 key-value pairs. With JSON, the keys are always strings, but the values can be objects (i.e. children JSON objects), arrays, numbers, or strings. The value set to the “message” key is “hi”, the value of the “age” key is the number 22, and the value of the “items” key is an array with two strings in it.

The JSON object itself is represented by a String type. By passing this data as a String, any language can easily re-create JSON object from the string as a constructor argument. Very convenient and very simple.

Each language has its own way of creating an object from this format. Since Unity 5.3, there exists a native method to create a JSON object from a JSON string. You will create a JSON representation of the high score of the player and then print it to the console. But you extend this logic by sending the JSON to a server.

The Game script has a method named SaveAsJSON that is hooked up to the Save As JSON button. Replace SaveAsJSON with the following code:

public void SaveAsJSON()
{
  Save save = CreateSaveGameObject();
  string json = JsonUtility.ToJson(save);

  Debug.Log("Saving as JSON: " + json);
}

This creates the Save instance like you did earlier. Then it creates a JSON string using the ToJSON method on the JsonUtility class. It then prints the output to console.

Start a game, hit a few targets, then press Escape to bring up the menu. Click the Save As JSON button, and you will see the JSON string you created:

Console output

If you want convert that JSON into a Save instance you would simply use:

Save save = JsonUtility.FromJson<Save>(json);

That is what you would do if you wanted to download a save file from the web and then load it into your game. But setting up a web server is a whole other process! For now, pat yourself on the back because you just learned a few techniques that will… save you some trouble in your next game (groan)!

Where to Go From Here?

You can download the final project files here.

You’ve now gained a powerful tool for creating great games by enabling your players to save and load their game through the magic of serialization. You’ve also learned what JSON is and how you could use it to implement cloud saving. You’ve also learned what PlayerPrefs is used for (settings!), and what it’s not used for (saving the game).

If you’re looking to get more rounded in Unity, we have a whole section of Unity tutorials over here, and you’re welcome to join us on the Unity forums. You can always leave a comment here if you have anything you’d like to say.

If you are a die-hard Unity fan and want to become a full fledged developer, then check out our book Unity Games by Tutorials where you will make 4 complete games from scratch. One of the chapters even goes over how to use JSON as a level loader!

If you have any questions or comments on this tutorial, please join the discussion below!