2016-10-06 104 views
1

我必須在iOS設備上爲Unity遊戲本地保存一些數據。 Unity提供Directory.createDirectory在iOS中創建文件而不是目錄

Application.persistentDataPath 

獲取公共目錄保存數據。並在控制檯上打印顯示這個返回iOS的路徑是正確的,即/ Users/userName/Library/Developer/CoreSimulator/Devices/******** - **** - **** - *** * - ************ /數據/集裝箱/數據/應用/ ************ - ******** - *** *********/Documents/

所以一個函數返回路徑和其他檢查目錄是否存在,如果不是,它應該創建一個目錄,但它創建沒有任何擴展名的文件。這裏是我的代碼

void savePose(Pose pose){ 

     if (!Directory.Exists(PoseManager.poseDirectoryPath())){ 
      Directory.CreateDirectory(PoseManager.poseDirectoryPath()); 
      //Above line creates a file 
      //by the name of "SavedPoses" without any extension 
     } 
     // rest of the code goes here 
    } 

    static string poseDirectoryPath() { 
     return Path.Combine(Application.persistentDataPath,"SavedPoses"); 
    } 

回答

1

可能sloutions:

Path.Combine(Application.persistentDataPath,"SavedPoses");savedPoses之前添加了反斜槓,而其他斜線則是正斜槓。也許這會導致iOS上的問題。嘗試原始字符串連接而不使用Path.Combine函數。

static string poseDirectoryPath() { 
    return Application.persistentDataPath + "/" + "SavedPoses"; 
} 

。如果Directory類不能正常工作,使用DirectoryInfo類。

private void savePose(Pose pose) 
{ 
    DirectoryInfo posePath = new DirectoryInfo(PoseManager.poseDirectoryPath()); 

    if (!posePath.Exists) 
    { 
     posePath.Create(); 
     Debug.Log("Directory created!"); 
    } 
} 

static string poseDirectoryPath() 
{ 
    return Path.Combine(Application.persistentDataPath, "SavedPoses"); 
} 

編輯

可能的iOS上的權限問題。

您可以在StreamingAssets目錄中創建文件夾。您有權讀取和寫入此目錄。

訪問此的一般方法是Application.streamingAssetsPath/

在iOS上,它也可以通過Application.dataPath + "/Raw"訪問。

在Android上,它也可以通過"jar:file://" + Application.dataPath + "!/assets/";訪問,Windows和Mac通過Application.dataPath + "/StreamingAssets";訪問。只需使用適合你的那個。

對於你的問題,Application.dataPath + "/Raw"+"/SavedPoses";應該這樣做。

+0

** 1。**這是我的原始代碼,在檢查其他問題後更改了它。正如我所說的,在兩種情況下(原始字符串和Path.Combine),生成的路徑都是正確的,正斜槓** 2 **這也會產生相同的問題。 – ibnetariq

+0

什麼是Unity版本,您正在測試的iOS版本是什麼? – Programmer

+0

Unity = 5.4.1f1,Xcode = 7.3.1,Simulator = iPhone 5S和iOS = 9.3。同樣的問題也發生在設備上。我正在使用模擬器,所以我可以從Finder查看文檔目錄 – ibnetariq

相關問題