【发布时间】:2015-08-08 12:31:59
【问题描述】:
在我的游戏中,我使用序列化将几个数组和变量保存到磁盘上的文件中。一切都很好。
但是在我尝试构建 iOS 之后,它拒绝保存,并且 Xcode 调试器说 - “文件名尚不支持”。
我该如何解决这个问题?
这是保存和加载数据的代码:
public void SaveState()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + saveFileName);
bf.Serialize(file, this);
file.Close();
}
public static GlobalState LoadState()
{
if (File.Exists(Application.persistentDataPath + saveFileName)) {
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + saveFileName, FileMode.Open);
GlobalState result = (GlobalState)bf.Deserialize(file);
file.Close();
return result;
}
return null;
}
这是我的序列化函数:
// Deserialization function
public GlobalState(SerializationInfo info, StreamingContext ctxt)
{
lastBossKilled = (int)info.GetValue("lastBoss", typeof(int));
currentlySelectedSpells = (SpellType[])info.GetValue("spells", typeof(SpellType[]));
learnedTalents = (int[])info.GetValue("talents", typeof(int[]));
talentPointsAvailable = (int)info.GetValue("talentPoints", typeof(int));
}
//Serialization function.
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("lastBoss", lastBossKilled);
info.AddValue("spells", currentlySelectedSpells);
info.AddValue("talents", learnedTalents);
info.AddValue("talentPoints", talentPointsAvailable);
}
我考虑过使用用户默认设置,但我无法在此处保存数组。
我的文件名是这样构造的:
private static string saveFileName = "045.bin";
new StreamWriter(Application.persistentDataPath + saveFileName)
【问题讨论】:
-
你的文件名是什么?
-
@JeanLuc 编辑的问题
-
我认为您只需将其修改为:
Application.persistentDataPath + "/" + saveFileName -
@JeanLuc 请创建一个答案,这有帮助!
标签: c# ios xcode unity3d il2cpp