1

在我的游戏中,我使用序列化将几个数组和变量保存到磁盘上的文件中。万事皆安。

但是在我尝试构建 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)
4

1 回答 1

1

文件路径中缺少斜杠将其更改为:

new StreamWriter(Application.persistentDataPath + "/" + saveFileName)

像 iOS 这样的严格沙箱系统会禁止这样做,因为它会在您的沙箱根文件夹旁边创建一个文件,但错误消息可能比“尚不支持文件名”更好,因为它缺少前导...

于 2015-08-10T20:37:05.093 回答