【Unity】Easy Data Saving in Unity: PlayerPrefs Usage and Considerations

Created: 2025-12-07

PlayerPrefs is a feature for saving simple data like volume settings and high scores. Learn basic usage (SetInt, GetInt), cases where you shouldn't use it, and the importance of encryption.

Overview

It's convenient when previous settings are restored upon launching a game. Volume settings, graphics quality, key configs, high scores—many games need features to save player preferences and simple progress data.

Unity provides the PlayerPrefs class for easily saving and loading such relatively simple data to devices. PlayerPrefs stores data as key-value pairs in platform-standard locations: registry (Windows), plist files (macOS/iOS), or XML files (Android).

This article covers basic data saving and loading with PlayerPrefs, plus the limitations and considerations hidden behind its convenience.

Basic PlayerPrefs Usage

PlayerPrefs supports only three data types: int, float, and string. Each type has corresponding Set methods (save) and Get methods (load).

Saving Data: Set Methods

Save data using SetInt(), SetFloat(), SetString() methods. The first argument is a key (string) identifying the data; the second is the value to save.

using UnityEngine;

public class GameSettings : MonoBehaviour
{
    public void SaveVolume(float volumeLevel)
    {
        // Save float volume data with key "MasterVolume"
        PlayerPrefs.SetFloat("MasterVolume", volumeLevel);
    }

    public void SaveHighScore(int score)
    {
        // Save int high score with key "HighScore"
        PlayerPrefs.SetInt("HighScore", score);
    }
}

Important: Calling Set methods only stores data in memory—it's not written to disk yet. To prevent data loss from crashes, call PlayerPrefs.Save() explicitly after saving important data to force disk writes.

public void SaveAllSettings()
{
    PlayerPrefs.SetInt("QualityLevel", 2);
    PlayerPrefs.SetFloat("MusicVolume", 0.8f);

    // Immediately write memory changes to disk
    PlayerPrefs.Save();
}

Loading Data: Get Methods

Load saved data using GetInt(), GetFloat(), GetString() methods. The first argument is the key used when saving.

Crucially, specify a default value as the second argument. When data for the specified key doesn't exist (e.g., first game launch), this default value is returned. Without a default, missing keys return 0 or empty strings, causing unintended behavior.

using UnityEngine;
using UnityEngine.UI;

public class LoadSettings : MonoBehaviour
{
    public Slider volumeSlider;
    public Text highScoreText;

    void Start()
    {
        // Load "MasterVolume" key data. Use default 1.0f if missing.
        float savedVolume = PlayerPrefs.GetFloat("MasterVolume", 1.0f);
        volumeSlider.value = savedVolume;

        // Load "HighScore" key data. Use default 0 if missing.
        int highScore = PlayerPrefs.GetInt("HighScore", 0);
        highScoreText.text = "High Score: " + highScore;
    }
}

Other Useful Methods

  • PlayerPrefs.HasKey(string key): Returns bool indicating whether data for the specified key exists. Useful for branching first-launch logic.
  • PlayerPrefs.DeleteKey(string key): Deletes data for the specified key.
  • PlayerPrefs.DeleteAll(): Deletes all PlayerPrefs data. Used for game reset features, but very powerful—handle with care.

PlayerPrefs Considerations and Limitations

While PlayerPrefs is convenient, it's not a universal solution. Understanding when not to use it is important.

1. Security Issues

Data saved with PlayerPrefs is not encrypted. Users with minimal knowledge can easily find and modify saved files. Saving important data like high scores or currency directly with PlayerPrefs is very risky.

Solutions:

  • For important data, always apply custom encryption before using PlayerPrefs.
  • Alternatively, skip PlayerPrefs and implement a more robust save system—write to JSON or binary files and encrypt those files.

2. Complex Data Storage

PlayerPrefs only handles int, float, and string. It's unsuitable for complex structured data like player inventory (item lists), character stats, or quest progress.

Solutions:

  • Use libraries like JsonUtility or Json.NET to convert (serialize) custom class instances to JSON strings, then save with PlayerPrefs.SetString(). This is a common, relatively easy approach.
  • For larger, more complex save data, creating dedicated save files (binary format, etc.) offers better performance and extensibility.

Summary

PlayerPrefs is ideal as an introduction to data persistence in Unity.

  • Easily save int, float, string types as key-value pairs.
  • Save with Set methods, load with Get methods.
  • Always specify default values in Get methods for missing data.
  • Data is not encrypted—unsuitable for data you don't want tampered with, like high scores.
  • For complex data structures, combine with JSON serialization.

Properly understanding PlayerPrefs strengths and weaknesses is key—use PlayerPrefs for simple options like volume settings, and more robust file-based systems for complex, important save data.