2017-10-13 223 views
1

我在Android中調試Unity遊戲,一切工作在Unity編輯器中。在Android上保存當前遊戲數據時,我收到UnauthorizedAccessException。Android Unity c#:UnauthorizedAccessException寫保存遊戲數據

我正在寫入persistentDataPath,所以我不明白爲什麼訪問被阻止。

下面是使用的logcat控制檯日誌:

<i>AndroidPlayer(motorola_Moto_G_(5)@192.168.0.26)</i> UnauthorizedAccessException: 
Access to the path "/current.sg" is denied. 
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x0028a] in /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.IO/FileStream.cs:320 
    at System.IO.FileStream..ctor (System.String path, FileMode mode) [0x00000] in <filename unknown>:0 
    at SaveLoadManager.SaveGame (.Game currentGame) [0x0002a] in F:\Work\Magister\Magister\Magister\Assets\Scripts\SaveLoadManager.cs:16 
    at PlayerController.FixedUpdate() [0x0058e] in F:\Work\Magister\Magister\Magister\Assets\Scripts\PlayerController.cs:244 

從的PlayerController在FixedUpdate()循環運行相關的代碼:

if (!gameSaved) 
{ 
    SaveLoadManager.SaveGame(gameManager.GetComponent<Game>()); 
    gameSaved = true; 
} 

遊戲存檔功能從SaveLoadManager:

public static void SaveGame(Game currentGame) 
{ 
    string saveGameFileName = Path.DirectorySeparatorChar + "current.sg"; 
    string filePath = Path.Combine(Application.persistentDataPath, saveGameFileName); 
    BinaryFormatter bf = new BinaryFormatter(); 
    FileStream file = new FileStream(filePath, FileMode.Create); 
    GameData data = new GameData(currentGame); 
    bf.Serialize(file, data); 
    file.Close(); 
} 

AndroidManifest.xml中的讀寫權限:

<uses-permission 
    android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
    android:maxSdkVersion="18" /> 

<uses-permission 
    android:name="android.permission.READ_EXTERNAL_STORAGE" 
    android:maxSdkVersion="18" /> 
+0

我想'Path.Combine'是治療'saveGameFileName'絕對路徑,這意味着你要寫入根目錄。嘗試將'string saveGameFileName = Path.DirectorySeparatorChar +「current.sg」'更改爲'string saveGameFileName =「current.sg」'。 –

回答

1

在寫入該路徑之前,您必須檢查目錄是否存在Directory.Exists。如果沒有,請使用Directory.CreateDirectory來創建它。

請注意,將文件直接保存到Application.persistentDataPath不是一個好主意。你必須先在裏面創建一個文件夾,然後把你的文件保存到該文件夾​​中。所以Application.persistentDataPath/yourcreatedfolder/yourfile.extension是好的。通過這樣做,你的代碼也可以在iOS上運行。

這是代碼應該是什麼樣子:

string saveGameFileName = "current"; 
string filePath = Path.Combine(Application.persistentDataPath, "data"); 
filePath = Path.Combine(filePath, saveGameFileName + ".sg"); 


//Create Directory if it does not exist 
if (!Directory.Exists(Path.GetDirectoryName(filePath))) 
{ 
    Directory.CreateDirectory(Path.GetDirectoryName(filePath)); 
} 

try 
{ 
    BinaryFormatter bf = new BinaryFormatter(); 
    FileStream file = new FileStream(filePath, FileMode.Create); 
    GameData data = new GameData(currentGame); 
    bf.Serialize(file, data); 
    file.Close(); 
    Debug.Log("Saved Data to: " + filePath.Replace("/", "\\")); 
} 
catch (Exception e) 
{ 
    Debug.LogWarning("Failed To Save Data to: " + filePath.Replace("/", "\\")); 
    Debug.LogWarning("Error: " + e.Message); 
}