2017-03-16 134 views
-1

我有一個文件,我可以保存最高分。在Unity編輯器中,路徑是:path = Application.dataPath + "/Score.json"; 我不知道如何在Android中獲取此文件的路徑。 錯誤已被刪除統一資產路徑Android。

+0

您保存爲使用'Application.persistentDataPath'而不是'Application.dataPath'。在嘗試保存之前,您還必須檢查文件或目錄是否存在。無論如何,您將遊戲數據保存爲json,我認爲這是重複的。 – Programmer

回答

1

Application.dataPath指向Android上的.apk文件,據我所知。我建議你在每個平臺上使用不同的路徑。這是我爲了節省一些遊戲而保存的東西。

public static string Path 
     { 
      get 
      { 
       switch (Application.platform) 
       { 
        case RuntimePlatform.IPhonePlayer: 
         return Path.Combine(Application.persistentDataPath, "MyStuff"); 

        case RuntimePlatform.Android: 
         return Path.Combine(Application.temporaryCachePath, "MyStuff"); 

        default: 
         return Path.Combine(Directory.GetParent(Application.dataPath).FullName, "MyStuff"); 
       } 
      } 
     } 
0

在Android上,Application.dataPath將指向APK所以你應該避免使用它保存數據,因爲你將無法寫入。相反,你想使用Application.persistentDataPath。作爲鏈接的文檔指出:

在iOS和Android上發佈,persistentDataPath將指向設備上的 公共目錄。隨着應用程序的每次更新,此位置中的文件將不會被刪除 。

+0

'#if UNITY_ANDROID \t \t path = Application.persistentDataPath +「/Score.json」; \t \t Debug.Log(「Android device」); #endif'是這樣嗎? – Karol

+0

@卡羅爾看起來不錯。 – Foggzie