【问题标题】:Unity 5.1 serialization on iOS - IL2CPP Filename not supported yetiOS 上的 Unity 5.1 序列化 - 尚不支持 IL2CPP 文件名
【发布时间】: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


【解决方案1】:

文件路径中缺少slash。改成这样:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多