Mathieu Dachy
Mathieu Dachy

Reputation: 27

How access to persistentDataPath child folders on IOS (Unity Deployment)?

I develop an app for iOS (Ipad precisely) and I need to save a config file on the Ipad to save user progression. The documentation of Unity says to use Application.persistentDataPath to make update on a config file on Ipad. So, I used this and it's works on Unity editor, but when I deploy the app on Xcode I get this error at the execution :

Cannot load file : "/var/mobile/Containers/Data/Application/.../Documents/Config/config.ini"

I work on Windows, so my Application.persistentDataPath is : "C:\Users\stagiaire\AppData\LocalLow\CompName\ProjectName"

At root of this structure, I have "Config/config.ini", so I expected that this configuration would be the same on iOS.

But when I check if these folders and/or files exist with :

if (Directory.Exists(UnityEngine.Application.persistentDataPath + "/Config/config.ini"))

XCode says that this file doesn't exists, but when I make this test :

if (Directory.Exists(UnityEngine.Application.persistentDataPath + "/"))

XCode says that this folder exists ??????

So, I don't understand. Unity create the structure of persistentDataPath and don't create the folders and files contained on this structure. It seems weird so I ask the question because I have no more idea to solve this problem.

Sorry for my poor english and thanks for your time.

Upvotes: 1

Views: 9552

Answers (1)

Kay
Kay

Reputation: 13156

Unity does not create a directory structure but instead gives you access to the Documents folder from the app's sandbox. That means you have to create your directories and files once when they are accessed the very first time.

string dir = Application.persistentDataPath + "/Config";
if (!Directory.Exists (dir)) {
    Directory.CreateDirectory (dir);
}
string configFile = dir + "/config.ini";
if (!File.Exists (configFile)) {
    File.Create (qcarFile);
}

BTW: If you call Directory.Exists () passing a file name it will return false as the file is not a directory.

Upvotes: 3

Related Questions