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?
Other Common Game Engines Questions (and Answers)
- Can You Use C# in Unreal Engine?
- Is Unreal Engine Open Source?
- Can Unreal Engine make 2D games?
- Does Unreal Engine Use Python?
- What Language Does Unreal Engine Use?
- Does Unreal Engine Use JavaScript?
- Does Unreal Engine work on Linux?
- How Do I Uninstall Unreal Engine 5?
- Is Blender or Unreal Engine Better?
- Does Unreal Engine Work on Mac?
- Why Is Unreal Engine So Laggy?
- Can I Run Unreal Engine 5 Without a Graphics Card?
Free System Design on AWS E-Book
Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.
Switch & save up to 80%
Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost