public void SavePersistentData(string id) { BinaryFormatter binaryFormatter = new BinaryFormatter(); SurrogateSelector surrogateSelector = new SurrogateSelector(); surrogateSelector.AddAllUnitySurrogate(); StorageCharacterItemSerializationSurrogate storageCharacterItemSS = new StorageCharacterItemSerializationSurrogate(); StorageSaveDataSerializationSurrogate storageSaveDataSS = new StorageSaveDataSerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(StorageCharacterItem), new StreamingContext(StreamingContextStates.All), storageCharacterItemSS); surrogateSelector.AddSurrogate(typeof(StorageSaveData), new StreamingContext(StreamingContextStates.All), storageSaveDataSS); binaryFormatter.SurrogateSelector = surrogateSelector; string path = Application.persistentDataPath + "/" + id + "_storage.sav"; FileStream file = File.Open(path, FileMode.OpenOrCreate); binaryFormatter.Serialize(file, this); file.Close(); }
public void LoadPersistentData(string id) { string path = Application.persistentDataPath + "/" + id + "_storage.sav"; storageItems.Clear(); if (File.Exists(path)) { BinaryFormatter binaryFormatter = new BinaryFormatter(); SurrogateSelector surrogateSelector = new SurrogateSelector(); surrogateSelector.AddAllUnitySurrogate(); StorageCharacterItemSerializationSurrogate storageCharacterItemSS = new StorageCharacterItemSerializationSurrogate(); StorageSaveDataSerializationSurrogate storageSaveDataSS = new StorageSaveDataSerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(StorageCharacterItem), new StreamingContext(StreamingContextStates.All), storageCharacterItemSS); surrogateSelector.AddSurrogate(typeof(StorageSaveData), new StreamingContext(StreamingContextStates.All), storageSaveDataSS); binaryFormatter.SurrogateSelector = surrogateSelector; FileStream file = File.Open(path, FileMode.Open); StorageSaveData result = (StorageSaveData)binaryFormatter.Deserialize(file); storageItems = result.storageItems; file.Close(); } }