When it comes to storing game data in Unity, there are several ways to do it — and choosing the right one is important. The data storage method you pick can directly affect your game’s efficiency, speed, and overall performance.
If you want to store static data like item stats, lists, or character configs, ScriptableObjects are a great choice.
If you want to store game saves, player progress, or structured runtime data, JSON/XML files are better.
If you’re dealing with large, queryable datasets, SQLite databases are ideal.
When you want to bundle data or assets with your build, Resources or Addressables are useful.
And if you want to prototype data storage or save non-sensitive information such as settings, control layouts, or the last completed level — PlayerPrefs is the best option.
One by one, we’ll explore each of these in upcoming blogs. Today, let’s start with PlayerPrefs.
What is PlayerPrefs?
PlayerPrefs is a Unity class that allows you to store small and simple pieces of data locally. It’s best used for quick, lightweight saves — not full save files or complex objects.
Internally, PlayerPrefs stores data in the local registry or system-specific storage, without any encryption. It’s great for prototyping or saving basic settings, but you should avoid using it for sensitive or large-scale game data.
You can store int, float, and string values directly. You can also serialize objects into JSON strings if needed.
When to Use PlayerPrefs
Use PlayerPrefs when you need to save:
- Game settings (volume, graphics quality, key bindings)
- Last level completed
- Player name or preferences
- Control layouts
- Other lightweight data
Avoid using it for:
- Inventories, save files, or complex structured data
- Sensitive information like login details or in-game purchases
Common Methods
PlayerPrefs exposes several static methods to handle data operations. Here’s an example script that demonstrates how to use them:
using UnityEngine;
public class GameSettingsManager : MonoBehaviour
{
// Example game settings
private float masterVolume = 0.8f;
private float brightness = 1.0f;
private int difficultyLevel = 2; // 0 = Easy, 1 = Medium, 2 = Hard
private void Start()
{
// Load settings on start
LoadSettings();
}
public void SaveSettings()
{
// Save individual settings
PlayerPrefs.SetFloat("MasterVolume", masterVolume);
PlayerPrefs.SetFloat("Brightness", brightness);
PlayerPrefs.SetInt("DifficultyLevel", difficultyLevel);
// Ensure data is written to disk
PlayerPrefs.Save();
Debug.Log("Settings saved!");
}
public void LoadSettings()
{
// Check if a key exists before reading
if (PlayerPrefs.HasKey("MasterVolume"))
masterVolume = PlayerPrefs.GetFloat("MasterVolume");
if (PlayerPrefs.HasKey("Brightness"))
brightness = PlayerPrefs.GetFloat("Brightness");
if (PlayerPrefs.HasKey("DifficultyLevel"))
difficultyLevel = PlayerPrefs.GetInt("DifficultyLevel");
Debug.Log($"Settings loaded:\nVolume: {masterVolume}\nBrightness: {brightness}\nDifficulty: {difficultyLevel}");
}
public void ResetSettings()
{
// Delete a single key
PlayerPrefs.DeleteKey("MasterVolume");
PlayerPrefs.DeleteKey("Brightness");
PlayerPrefs.DeleteKey("DifficultyLevel");
Debug.Log("Individual settings deleted!");
}
public void DeleteAllSettings()
{
// Delete everything saved by PlayerPrefs
PlayerPrefs.DeleteAll();
Debug.Log("All PlayerPrefs cleared!");
}
// Example for toggling between difficulty levels
public void CycleDifficulty()
{
difficultyLevel = (difficultyLevel + 1) % 3;
SaveSettings();
}
}
Where Unity Stores PlayerPrefs
Unity stores PlayerPrefs in different locations depending on the operating system:
- Windows: Registry — HKEY_CURRENT_USER\Software[CompanyName][ProductName]
- MacOS: ~/Library/Preferences/com.[CompanyName].[ProductName].plist
- Android: /data/data/[pkg-name]/shared_prefs/[pkg-name].v2.playerprefs.xml
- iOS: Uses [NSUserDefaults standardUserDefaults] API
- Linux: ~/.config/unity3d/[CompanyName]/[ProductName]
- WebGL: Stored in the browser’s IndexedDB, up to 1MB
Key Takeaway
PlayerPrefs is a quick and easy way to store small pieces of data in Unity. It’s perfect for prototyping, player settings, and preferences — but not ideal for complex or sensitive data. In the next part, we’ll explore other data storage methods like JSON files and ScriptableObjects to handle larger or more structured data.
Top comments (0)