Creating a new directory folder in _Data after build

Hi guys,

I want to create with C# a script that creates a new folder in the ProjectName_Data folder because I want to create files in the game and I want to locate this in game too.
Can you help me please with this 2 doubts?

  • How can create a folder in _Data?
  • How can access it from the game?

Thank you!

There’s two special folders set aside for this:

  • Data/StreamingAssets is a folder in your data folder, and stuff put there will not be considered as game files, but normal files to be loaded as usual.
  • PersistentDataPath is in Users/Appdata, and is based on the name of your company and game, which you enter in the top of the player settings. This folder does not change between builds, so you can re-use the assets put there from different builds.

For both folders, Unity gives you the path through:
Application.streamingAssetsPath
and
Application.persistentDataPath

And you can use the normal C# File and Directory classes to work with files and directories in there:

Directory.CreateDirectory(Application.streamingAssetsPath + "/Test");
File.WriteAllText(Application.streamingAssetsPath + "/Test/file.txt", "this is some text!");

EDIT: Note that I have no idea if this works on mobile platforms. It’ll probably not work on WebGL.

5 Likes