Sunday, January 12, 2020

Unity PlayerPrefs

A straightforward tutorial on how to use PlayerPrefs

What is PlayerPrefs?

  • PlayerPrefs is a persistent data that is stored in the user's hard disk. This means that the value in it will remain even the game was closed and re-opened.
  • PlayerPrefs is usually used to save values from the game such as in-game settings. Others use it to save in-game progress for small games.

Where do PlayerPrefs values store?

  • In windows, PlayerPrefs values are being stored in the registry. Specifically in HKEY_CURRENT_USER\Software\Unity\UnityEditor\(Company Name)\(Game Name)

What type of values can you store in PlayerPrefs?

  • There are 3 different data types you can store in PlayerPrefs:
    • String
    • int
    • float

How to use PlayerPrefs?


Sample Script name YourScript

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
using UnityEngine;
     
public class YourScript : MonoBehavior
{
 void Start()
 {
  // store 3 different data types in PlayerPrefs
  PlayerPrefs.SetString("player_name","Shrieg");
  PlayerPrefs.SetInt("player_hp",100);
  PlayerPrefs.SetFloat("player_money",250.70f);
  PlayerPrefs.Save(); // required to save values
  
  // access stored PlayerPrefs
  Debug.log("Player Name: " + PlayerPrefs.GetString("player_name"));
  Debug.log("Player HP: " + PlayerPrefs.GetInt("player_hp"));
  Debug.log("Player Money: " + PlayerPrefs.GetFloat("player_money"));
 }
}

PlayerPrefs syntax: PlayerPrefs.SetDataType("key",value)
PlayerPrefs key must always be enclosed with " (double quotes).

How to check if a PlayerPrefs variable was stored?


To verify if the value was successfully stored. Use the HasKey("key") method of the PlayerPrefs class. It will return boolean True if the key exists, false if not.

Example: 
  • PlayerPrefs.HasKey("player_name"); // returns true
  • PlayerPrefs.HasKey("player_class"); // returns false

How to clear all PlayerPrefs?


In the Unity editor, navigate to the menu bar, click Edit -> Clear all PlayerPrefs and hit Yes once prompted. Note that this cannot be undone. It will clear stored PlayerPrefs registry records.

If you want to do it via script just simply write: PlayerPrefs.DeleteAll()

Can I rely on PlayerPrefs to Save game progress?


This question depends on what values you are storing in PlayerPrefs. PlayerPrefs values can easily be changed in windows registry, so if you store the player's attack power value as PlayerPrefs for example, one can easily modify the value to 9999, thus dealing maximum damage to enemies.

The best way to protect saved data is to use data encryption but that will be discussed in a different post.

So, what's the best way to store in-game values then? In my case, I use SQLite for storing persistent data. You can also use XML and/or other formats.


For more information about PlayerPrefs. See Unity's official documentation. https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

If you find this helpful, consider buying me a coffee? Thanks


Thank you and have a great day!

No comments:

Post a Comment