Je débogue un jeu Unity dans Android, tout fonctionne dans l'éditeur Unity. Je reçois une exception UnauthorizedAccessException lors de l'enregistrement des données de jeu en cours sur Android. J'écris à persistDataDataPath, donc je ne comprends pas pourquoi l'accès est bloqué.Android Unity C#: UnauthorizedAccessException écriture sauvegarde des données de jeu
Voici le journal de la console en utilisant 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
Le code pertinent de PlayerController en cours d'exécution dans une boucle FixedUpdate():
if (!gameSaved)
{
SaveLoadManager.SaveGame(gameManager.GetComponent<Game>());
gameSaved = true;
}
La fonction SaveGame de 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();
}
Les autorisations de lecture/écriture dans le fichier 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" />
Je pense que '' Path.Combine' traite saveGameFileName' comme un chemin absolu, ce qui signifie que vous essayez d'écrire dans le répertoire racine. Essayez de changer 'string saveGameFileName = Path.DirectorySeparatorChar +" current.sg "' en juste 'string saveGameFileName =" current.sg "'. –