File.Create在iPhone 5S上失败 [英] File.Create fails on iPhone 5S
问题描述
我正在研究保存/加载数据的Unity游戏.我正处于该项目的测试阶段,遇到一种不寻常的情况,即游戏拒绝保存在iOS中.在Android,Windows和OS X上运行时,它工作得很好.这是我的适用代码:
I'm working on a Unity game that saves/loads data. I am in the testing phase for this project, and ran into an unusual circumstance in which the game refuses to save in iOS. It works just fine when run on Android, Windows, and OS X. Here is my applicable code:
FileStream file = File.Create(GetSavePath());
bf.Serialize(file, saveFile);
file.Close();
我对GetSavePath()
的定义如下:
private string GetSavePath()
{
return Path.Combine(Application.persistentDataPath, SAVEPATH);
}
我将SAVEPATH
定义为SaveFile.bin
.请注意,问题出在File.Create()
上,如下面的跟踪所示:
I define SAVEPATH
as SaveFile.bin
. Please note that the issue is explicitly with File.Create()
, as seen in the trace below:
IOException: Sharing violation on path /var/mobile/Containers/Data/Application/4AE45A80-AC36-4A96-9C69-BE01CB753896/Documents/SaveFile.bin
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) [0x00000] in <filename unknown>:0
at System.IO.File.Create (System.String path, Int32 bufferSize) [0x00000] in <filename unknown>:0
at System.IO.File.Create (System.String path) [0x00000] in <filename unknown>:0
at SaveManager.Save () [0x00000] in <filename unknown>:0
at SaveManager.NewSave () [0x00000] in <filename unknown>:0
at GameManager.NewSave () [0x00000] in <filename unknown>:0
at TitleManager.NewGame () [0x00000] in <filename unknown>:0
at UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) [0x00000] in <filename unknown>:0
从以上所述,应该注意,由于用户必须单击按钮才能创建新游戏,因此调用了Invoke
.跟踪中System.IO.File.Create
的问题与上面的FileStream file = File.Create(GetSavePath());
对应.
From the above, it should be noted that Invoke
is called since the user must click a button to create a new game. The issue with System.IO.File.Create
in the trace corresponds to the FileStream file = File.Create(GetSavePath());
above.
设备信息:
- iPhone 5S
- iOS 10.1
统一版本:5.4.1f1
Unity version: 5.4.1f1
我尚未在其他iOS设备上进行测试.
I have yet to test on other iOS devices.
推荐答案
在FileMode.OpenOrCreate
模式下使用File.Open()
而不是File.Create()
.当前的猜测是File.Create()
无法立即释放该文件的锁定,从而阻止返回的文件流访问该文件.
Use File.Open()
with the mode FileMode.OpenOrCreate
instead of File.Create()
. Current speculation is that File.Create()
fails to immediately release a lock on the file preventing the returned filestream from accessing the file.
这篇关于File.Create在iPhone 5S上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!