Android Unity c#:写入保存游戏数据的 UnauthorizedAccessException
Android Unity c# : UnauthorizedAccessException writing save game data
我正在 Android 中调试 Unity 游戏,在 Unity 编辑器中一切正常。我在 Android 上保存当前游戏数据时收到 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 的 SaveGame 函数:
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();
}
read/write权限在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
创建它。
请注意,将文件直接保存到 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);
}
我正在 Android 中调试 Unity 游戏,在 Unity 编辑器中一切正常。我在 Android 上保存当前游戏数据时收到 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 的 SaveGame 函数:
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();
}
read/write权限在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
创建它。
请注意,将文件直接保存到 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);
}