Saving Data In Unity (PlayerPrefs)

In this article, we will learn about the PlayerPrefs class. PlayerPrefs is used to burn the user’s data to disk and make it permanent. I hope to improve your game development career after this post, I hope it helps.

What Is Save System?

Even if the game is closed, we may have to use some user data later, in this case, the save system is used for that.

Unity has some great functions for the saving process so that we are protected from a lot of tedious lines of code.

The main logic of the recording system is the process of withdrawing data from that file by writing the data received from the user as a file on the disk.

Save Functions In Unity (PlayerPrefs)

Now we will learn to save variables, then we will see how to delete, recall and change this data from the disk.

PlayerPrefs.SetInt("Name" , 1);
PlayerPrefs.SetString("Name" , "X");
PlayerPrefs.SetFloat("Name" , 1.0f);

This function is used to write new data to disk. When using this function, we add the name to be saved first and then the value.

PlayerPrefs.GetInt("Name" , 0);
PlayerPrefs.GetString("Name" , " ");
PlayerPrefs.GetFloat("Name" , 0.0f);

With this function, we can recall previously saved data. The data next to it is the data to be called by default.

PlayerPrefs.DeleteAll();
PlayerPrefs.DeleteKey("KeyName");

You can use the Delete All function to delete all the data you have saved, but Delete Key is ideal if you want to delete only 1 data.

Tips And Information About Save System

We talked about the need to save a lot of data for the user in games, so what are these data, and why programmers do need to save this data?

Why Programmers Do Need To Save This Data?

In the background of most of the games, continuous processing takes place and these processes are usually saved in variables. However, some data is vital and should be available afterward even when the program is closed.

PlayerPrefs is just for this job, it helps the player to resume the game as it was last saved when the game is restarted, even if it closes the program or even deletes it (depending on where you saved it) by writing important variables to disk.

It is very important not only for the player’s progress in the game, but also for the programmer to store data that must be permanent for other scripts.

What Are These Data?

We learned why data is saved in the game and why it is important, so what are we going to record in the game?

There is a simple answer to this question, we can save all data. But the main point here is that we do not exploit the player’s disc.

While important data is saved in most games, almost everything is recorded in most games, the real point here is the size of the game. If your game is a big game, the data usage will be more and the data you need to save will increase accordingly.

Suggestion: Unless the data you will save is vital, do not save, too much disk usage may cause the player to delete the game.

Using PlayerPrefs In Unity

The most recorded data in a simple game is the health value, let’s learn how to save health data and recall it later.

Let’s create a class for the character and add the properties of the character to this class, then save this data in another script.

using UnityEngine;

public class Player: MonoBehaviour
{
  public static int Health;

  public static void Save()
  {
   PlayerPrefs.SetInt("Health",Health);
  }
}

You can use the data in public if you want. Let’s save this data when we the press S button and print it back when the press Enter.

using UnityEngine;

public class Save: MonoBehaviour
{
    void Update()
    {
     // When Press Space Increase Health
     if (Input.GetKey(KeyCode.Space))
     {
      Player.Health -= 10;
     }
     
     // When Press S Save Health Variable
     else if (Input.GetKey(KeyCode.S))
     {
      Player.Save();
      Debug.Log("Health:" + Player.Health);
     }
     
     // When Press Enter Write Health Value
     else if (Input.GetKeyDown(KeyCode.Return))
     {
      Debug.Log(Player.Health);
     }
   }
}

Note: Reduce the use of static fields in your project, here I used it to reduce code confusion, but using too many static fields in a real project can cause performance problems.

Leave a Reply

Your email address will not be published. Required fields are marked *