Which is the root directory anyway?

I’m trying to implement a save/load functionality and I’m having trouble even getting started.

Here’s what I want to accomplish.

  1. Have a default save data template. This is stored in my Assets/Scripts/Static folder
  2. When game loads, it looks for a file called Savedata.json in the root* directory.
  3. It Savedata.json exists, load save data from that file.
  4. If Savedata.json does not exist, copy the default save data template from the Assets/Scripts/Static folder

The trouble is, I don’t have the slightest idea what the *root folder is. Where is it? How do I set a path to it?

Right now, I’m doing this to copy the default template

System.IO.File.Copy("Assets/Scripts/Static/defaultSavedata.json", "Documents/Savedata.json");

But I’m getting a console error saying

DirectoryNotFoundException: Destination directory not found: Documents System.IO.File.Copy (System.String sourceFileName, System.String destFileName, System.Boolean overwrite) (at <9577ac7a62ef43179789031239ba8798>:0) System.IO.File.Copy (System.String sourceFileName, System.String destFileName) (at <9577ac7a62ef43179789031239ba8798>:0) MenuUIHandler.initializeSaveData () (at Assets/Scripts/MenuUIHandler.cs:40) MenuUIHandler.Start () (at Assets/Scripts/MenuUIHandler.cs:17)

Uhm, you do know that the Assets folder is a project folder. It does not exist in the built game. So any kind of save data has nothing to do with the authoring / creation process of the game. Apart from that Unity provides runtime storage locations that the OS recommends (or requires) through various folder paths. A good example is that an application that is “properly” installed and sits in the programs folder does not have write access to that folder as you need admin privileges to do so. Applications should store their runtime data inside their own subfolder of the %APPDATA% folder. That folder path is returned by Application.persistentDataPath. It is constructed by your company name and product name (that you specified in the player settings).

As I said the Assets folder does not exist at runtime. To read your template (and to actually include the template in the build) you have to place it either in a Resources folder / subfolder, inside StreamingAssets or reference it through a TextAsset reference directly.

The persistent data path is a storage location where you can read and write files you want to persist (as the name suggests). This path should work on pretty much all platforms, AFAIK even WebGL where the IndexedDB and FileSystem API is used.

So you shouldn’t worry too much about where exactly the persistent data is stored as this is vastly different on different platforms.