Question: Where Is Unitys PersistentDataPath?

Answer

Application.persistentDataPath is a string property in Unity that gets the path to a directory where you can store data that should persist between sessions. This directory is not cleared except by deleting the app itself. It is safe to use this directory for saving game states, settings, user profiles, etc.

The location of persistentDataPath varies depending on the platform:

  • On iOS: It maps to <Application_Home>/Documents. This is backed up by iTunes and iCloud.
  • On Android: It points to /storage/emulated/0/Android/data/<packagename>/files which is a private folder accessible only to the app. It is deleted when the user uninstalls the app.
  • On Windows: It typically points to C:\Users\<username>\AppData\LocalLow\<companyname>\<productname>.
  • On macOS: The path usually is ~/Library/Application Support/<companyname>/<productname>.

Here is an example of how you might access and use Application.persistentDataPath in your code:

using System.IO; using UnityEngine; public class DataPersistenceExample : MonoBehaviour { private string filePath; void Start() { // Define the file path where data will be stored. filePath = Path.Combine(Application.persistentDataPath, "gameSave.txt"); // Write to the persistent data path. SaveToFile("This is persisted game data."); // Read from the persistent data path. string savedData = LoadFromFile(); Debug.Log(savedData); } void SaveToFile(string data) { File.WriteAllText(filePath, data); } string LoadFromFile() { if (File.Exists(filePath)) { return File.ReadAllText(filePath); } else { Debug.LogError("File not found"); return ""; } } }

In this example, we're combining Application.persistentDataPath with a filename to create a full path to a text file. We then have functions to save data to this file and to load data from it. This approach ensures that the data persists across game sessions and even after the application restarts.

Was this content helpful?

Start building today

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