Пример #1
0
    public void SavePersistentData(string id, string map)
    {
        BinaryFormatter   binaryFormatter   = new BinaryFormatter();
        SurrogateSelector surrogateSelector = new SurrogateSelector();

        surrogateSelector.AddAllUnitySurrogate();
        BuildingSaveDataSerializationSurrogate buildingSaveDataSS = new BuildingSaveDataSerializationSurrogate();
        WorldSaveDataSerializationSurrogate    worldSaveDataSS    = new WorldSaveDataSerializationSurrogate();

        surrogateSelector.AddSurrogate(typeof(BuildingSaveData), new StreamingContext(StreamingContextStates.All), buildingSaveDataSS);
        surrogateSelector.AddSurrogate(typeof(WorldSaveData), new StreamingContext(StreamingContextStates.All), worldSaveDataSS);
        binaryFormatter.SurrogateSelector = surrogateSelector;
        string     path = Application.persistentDataPath + "/" + id + "_world_" + map + ".sav";
        FileStream file = File.Open(path, FileMode.OpenOrCreate);

        binaryFormatter.Serialize(file, this);
        file.Close();
    }
Пример #2
0
    public void LoadPersistentData(string id, string map)
    {
        var path = Application.persistentDataPath + "/" + id + "_world_" + map + ".sav";

        if (File.Exists(path))
        {
            var binaryFormatter   = new BinaryFormatter();
            var surrogateSelector = new SurrogateSelector();
            surrogateSelector.AddAllUnitySurrogate();
            var buildingSaveDataSS = new BuildingSaveDataSerializationSurrogate();
            var worldSaveDataSS    = new WorldSaveDataSerializationSurrogate();
            surrogateSelector.AddSurrogate(typeof(BuildingSaveData), new StreamingContext(StreamingContextStates.All), buildingSaveDataSS);
            surrogateSelector.AddSurrogate(typeof(WorldSaveData), new StreamingContext(StreamingContextStates.All), worldSaveDataSS);
            binaryFormatter.SurrogateSelector = surrogateSelector;
            var file   = File.Open(path, FileMode.Open);
            var result = (WorldSaveData)binaryFormatter.Deserialize(file);
            buildings = result.buildings;
            file.Close();
        }
    }