Search code examples
c#iosxcodeunity-game-engineil2cpp

Unity 5.1 serialization on iOS - IL2CPP Filename not supported yet


In my game I use serialization to save couple arrays and variables to file on disk. Everything is good.

But after I tried making an iOS build, it refuses to save, and Xcode debugger says - "Filename is not yet supported".

How do I fix this?

Here is the code that saves and loads the data:

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;
}

And here are my serialization functions:

// 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);
}

I thought about moving to user defaults, but I can't save arrays there.

My filename is constructed like this:

private static string saveFileName = "045.bin";
new StreamWriter(Application.persistentDataPath + saveFileName)

Solution

  • the slash is missing in the file path. change it to this:

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

    A strict sandbox system like iOS will forbid this because it would create a file next to your sandbox root folder, but the error message could have been better then "Filename is not yet supported", which is rather missing leading...