本文介绍了Android Unity C#:UnauthorizedAccessException编写保存游戏数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Android调试Unity游戏,一切都可以在Unity编辑器中进行.在Android上保存当前游戏数据时,我收到UnauthorizedAccessException.

I'm debugging a Unity game in Android, everything works in the Unity editor. I'm receiving an UnauthorizedAccessException when saving the current game data on Android.

我正在写persistentDataPath,所以我不明白为什么访问被阻止.

I am writing to the persistentDataPath so I don't understand why access is being blocked.

这是使用Logcat的控制台日志:

Here is the console log using 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

在FixedUpdate()循环中运行的PlayerController中的相关代码:

The relevant code from PlayerController running in a FixedUpdate() loop:

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

SaveLoadManager中的SaveGame函数:

The SaveGame function from 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中的读/写权限:

The read/write permissions in the 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" />

推荐答案

在写入该路径之前,必须检查目录是否存在Directory.Exists.如果不是,请使用Directory.CreateDirectory创建它.

You have to check if the directory exist with Directory.Exists before writing to that path. If it does not, use Directory.CreateDirectory to create it.

请注意,将文件直接保存到Application.persistentDataPath不是一个好主意.您必须先在其中创建一个文件夹,然后再将文件保存在该文件夹中.所以Application.persistentDataPath/yourcreatedfolder/yourfile.extension很好.这样,您的代码也将在iOS上运行.

Note that it's not a good idea to save file directly to Application.persistentDataPath. You have to create a folder inside it first then put save your file inside that folder. So Application.persistentDataPath/yourcreatedfolder/yourfile.extension is fine. By doing this, your code will also work on 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);
}

这篇关于Android Unity C#:UnauthorizedAccessException编写保存游戏数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:20