Introducing Dragonfly Cloud! Learn More

Question: Where are PlayerPrefs stored in Unity?

Answer

PlayerPrefs is a class in Unity that is used for storing and accessing player preferences between game sessions. Here's where PlayerPrefs data is typically stored on different platforms:

  • Windows: Data is saved in the registry under a subkey of HKEY_CURRENT_USER\Software\[CompanyName]\[ProductName], where [CompanyName] and [ProductName] are the company and product names you've set in your project settings.
Registry Example: HKEY_CURRENT_USER\Software\MyCompany\MyGame
  • macOS: PlayerPrefs are stored in ~/Library/Preferences/folder, the folder being named as the reverse DNS style bundle identifier (com.company.product).
File Example: ~/Library/Preferences/com.mycompany.mygame.plist
  • Linux: Data is stored in ~/.config/unity3d/[CompanyName]/[ProductName].
File Example: ~/.config/unity3d/MyCompany/MyGame/prefs
  • Android: PlayerPrefs uses SharedPreferences. The data is saved in an XML file in the app's private folder, located at /data/data/pkg-name/shared_prefs/pkg-name.xml, where pkg-name is your application's package name.

  • iOS: PlayerPrefs data is stored in NSUserDefaults and is backed by a .plist file within the app's sandbox.

  • WebGL: PlayerPrefs data is stored in the browser's IndexedDB.

Please note that while accessing or modifying PlayerPrefs is straightforward through the PlayerPrefs class methods like SetInt, GetInt, SetString, GetString, HasKey, etc., directly manipulating the storage files (registry entries for Windows, plist files for macOS, etc.) is not recommended as it could lead to data corruption or unexpected behavior in your game.

It is also important to understand that PlayerPrefs is meant for lightweight data storage and is not secure. Users with access to these paths can easily read or write to the PlayerPrefs data. Therefore, sensitive information should never be stored in PlayerPrefs.

Here's an example of using PlayerPrefs to store and retrieve a high score in Unity:

// To save a high score: PlayerPrefs.SetInt("HighScore", 1000); PlayerPrefs.Save(); // this is optional and mainly useful on web platforms // To retrieve a high score: int highScore = PlayerPrefs.GetInt("HighScore", 0); // 0 is the default value if not set

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.