Facebook

persistentDataPath Explained: How to Store Data in Unity

by Michael Sacco Published August 24, 2023
The cover for persistentDataPath Explained: How to Store Data in Unity

Hi! I’m Michael. I’m a game developer. I make assets and tools to help you develop better games, faster.

I’m writing this guide to teach you about a powerful tool in Unity. You will use this all the time when saving and loading data in Unity. This tool is the persistentDataPath.

The persistent data path tells you the best place to save player data. I always use this path to save and load data in Unity. You will, too.

This guide will not cover how to save or load data. But it will cover everything you need to know about persistentDataPath.

This is a series on data storage and user preferences in Unity. We recommend reading the series in order.

  1. Persistent Data Path in Unity
  2. Scriptable Objects in Unity
  3. Player Settings in Unity

Key Info

  • Application.persistentDataPath returns a string path.
  • This path is the best place for you to save data.
  • You need to save data to disk to preserve player data between game sessions.
  • The path is different for each platform. Windows, Mac, iOS, Android, and WebGL have different persistent data paths.
  • It doesn’t matter what the persistent data path is. But you need to save and load your data from the same place every time.

Are you a beginner game developer? Do you want to learn the basics of working with Unity? If so, then you’re in the right place.

You may still learn something new if you’re an intermediate or advanced game developer. Off we go.

What is a persistent data path?

It is a folder. This folder is the best place for you to save data in your game. As a game developer, you need to maintain player progress between sessions. You need to record when the player levels up. You need to save if the player completes a level. And you need to make sure that you preserve the player’s graphics settings.

Have you ever lost your saved data? If so, you know precisely why you need to save and load data with care.

Imagine you made a game. So you sent a copy to your friend. Your friend plays for hours. They get pretty good, and they make a lot of progress. Then, they close the game to eat dinner. When they come back, they open the game up again. And their progress is gone.

With persistentDataPath, you can save their work when they exit the game. Ready to pick up for their next gaming session.

How do I access the persistent data path?

To access this path, you only need a few lines of code. Here’s a simple example in C#:

string dataPath = Application.persistentDataPath;

This line of code gets the path where your game can store persistent data. It’s like asking Unity for the best place to store long-term data. This path is different for each platform.

How can I find the persistent data path location?

You can review the Unity documentation. But, I have found that their documentation could be more accurate. So, I recommend that you check yourself. Add the following script to your project. Then, confirm the data path in a build on your target platform.

using UnityEngine;

public class GetPersistentDataPath : MonoBehaviour

{
  void Start()
  {
    Debug.Log($"The persistent data path on {Application.platform} is located at: {Application.persistentDataPath}");
  }
}

Unity has documented the persistentDataPath location for each platform on their manual. If you trust them, take their word for it.

Does persistentDataPath work on every platform?

No. This property works on most platforms. Supported platforms include Windows, Mac, WebGL, iOS, and Android. Be cautious and check your target systems, though. Remember that this property does not work on some uncommon platforms.

Which platforms does persistentDataPath work on?

Persistent Data Path works on most platforms. I made a list of the platforms that it definitely works on.

Desktop:

  • Windows
  • Linux
  • Mac

Web:

  • WebGL

Console:

  • Switch
  • Xbox
  • PlayStation

Mobile:

  • iOS
  • Android

Which platforms does persistentDataPath not support?

Persistent Data Path doesn’t work on some platforms. As far as I know, there is one platform to watch out for. That platform is tvOS. Are you making a game for Apple TV? No? Then you have nothing to worry about.

Television:

  • tvOS (Apple TV)

Does persistentDataPath work on itch.io?

Yes and no. Mostly no.

Let’s say that you made a game on itch.io. If you check the data path, itch.io will return a path for that game.

But then, you update your game. Itch.io will produce a different path for the new version.

In other words, the path changes when you update your game.

You have nothing to fear if you make a game and leave it alone. But you must be careful if you plan to update your game. It will be hard to migrate your player data to the new path.

How can I save and load data in Unity on itch.io?

I did some digging here. You can work around this issue. To work around it, you create a custom directory in the IDBFS. That is short for indexed database filesystem. Then, you save your data to that directory. Untested.

This approach makes a lot of sense. But you need to apply it from the very beginning. It is possible to update your game path after launch. That being said, it will be difficult to do it.

using UnityEngine;
using System.IO;
using System.Runtime.InteropServices;

// References:
// https://www.reddit.com/r/UnityHelp/comments/sk3juk/old_game_save_gone_after_a_new_webgl_build_is/
// https://forum.unity.com/threads/how-does-saving-work-in-webgl.390385/
// https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
// https://prasetion.medium.com/saving-data-as-json-in-unity-4419042d1334

public class SaveWebGLData : MonoBehaviour
{
  const string saveDirectory = "idbfs/my-game";
  const string saveName = "my-save-data.json";
  private SaveData saveData = new SaveData();
  [DllImport("__Internal")]
  private static extern void JS_FileSystem_Sync();
  
  void Start()
  {
    if (!Directory.Exists(saveDirectory))
    {
      Directory.CreateDirectory(saveDirectory);
    }
  
  // Encode data to JSON string
  string jsonData = JsonUtility.ToJson(saveData);
  
  // Create the full file path
  string filePath = Path.Combine(saveDirectory, saveName);
  
  // Write data to the file system
  File.WriteAllText(filePath, jsonData);
  // Force the browser filesystem to sync with the system
  
  JS_FileSystem_Sync();
  }
  
  [System.Serializable]
  class SaveData
  {
    public int playerLevel;
  }
}

What is Serialization?

Serialization is the process of encoding data to a format that you can easily share and store on disk. That’s part of what happens when you use Unity’s Serialize Field attribute.

Can I save a file to the persistent data path?

Yes. This is the most common use case for the persistent data path. Saving and loading data is the most common operation.

Try it yourself

Practice makes perfect. It’s your turn to drill your new skills. I will give you a quick trial. This trial will help to make sure you understand this property…

  • Create a simple project where you write the value for the persistentDataPath.
  • Spend a few minutes thinking. What value will it get for your platform?
  • Run your project in the editor. What did it say?
  • Run your project in a build. Did the path change?

Recommended reading

https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html

https://prasetion.medium.com/saving-data-as-json-in-unity-4419042d1334

https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

https://forum.unity.com/threads/how-does-saving-work-in-webgl.390385/

https://www.reddit.com/r/UnityHelp/comments/sk3juk/old_game_save_gone_after_a_new_webgl_build_is/

Conclusion

Every game requires you to save and load data. As a game developer, you must know how to use the persistent data path. It provides a straightforward and consistent way to handle local storage. Managing local storage is core to saving and loading data in Unity. You need to save player progress and data between game sessions.

You now have a basic understanding of the persistent data path. Now, get up to speed on global variables with our comprehensive guide.

Free download: Indie Game Marketing Checklist

Download now

Category

unity basics

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free