public static void AddAllUnitySurrogate(this SurrogateSelector surrogateSelector) { var colorSS = new ColorSerializationSurrogate(); var quaternionSS = new QuaternionSerializationSurrogate(); var vector2IntSS = new Vector2IntSerializationSurrogate(); var vector2SS = new Vector2SerializationSurrogate(); var vector3IntSS = new Vector3IntSerializationSurrogate(); var vector3SS = new Vector3SerializationSurrogate(); var vector4SS = new Vector4SerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), colorSS); surrogateSelector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), quaternionSS); surrogateSelector.AddSurrogate(typeof(Vector2Int), new StreamingContext(StreamingContextStates.All), vector2IntSS); surrogateSelector.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), vector2SS); surrogateSelector.AddSurrogate(typeof(Vector3Int), new StreamingContext(StreamingContextStates.All), vector3IntSS); surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SS); surrogateSelector.AddSurrogate(typeof(Vector4), new StreamingContext(StreamingContextStates.All), vector4SS); }
// Loads the game with specifed filename private SaveData LoadGame(string _filename) { string path = Application.persistentDataPath + "/" + _filename + ".dat"; // Open Filestream FileStream file_stream; if (File.Exists(path)) { file_stream = File.OpenRead(path); } else { Debug.LogWarning("Save file not found"); return(null); } BinaryFormatter binary_formatter = new BinaryFormatter(); SurrogateSelector surrogate_selector = new SurrogateSelector(); // Custom selectors Vector2IntSerializationSurrogate vector2i_selector = new Vector2IntSerializationSurrogate(); Vector3SerializationSurrogate vector3_selector = new Vector3SerializationSurrogate(); // Add surrogate_selector.AddSurrogate(typeof(Vector2Int), new StreamingContext(StreamingContextStates.All), vector2i_selector); surrogate_selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3_selector); binary_formatter.SurrogateSelector = surrogate_selector; // Load file SaveData loaded_data = (SaveData)binary_formatter.Deserialize(file_stream); file_stream.Close(); return(loaded_data); }
// Saves the game with the specified filename private void SaveGame(string _filename) { string path = Application.persistentDataPath + "/" + _filename + ".dat"; // Open Filestream FileStream file_stream; if (File.Exists(path)) { file_stream = File.OpenWrite(path); } else { file_stream = File.Create(path); } BinaryFormatter binary_formatter = new BinaryFormatter(); SurrogateSelector surrogate_selector = new SurrogateSelector(); // Custom serialisation surrogates Vector2IntSerializationSurrogate vector2i_selector = new Vector2IntSerializationSurrogate(); Vector3SerializationSurrogate vector3_selector = new Vector3SerializationSurrogate(); // Add surrogate_selector.AddSurrogate(typeof(Vector2Int), new StreamingContext(StreamingContextStates.All), vector2i_selector); surrogate_selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3_selector); binary_formatter.SurrogateSelector = surrogate_selector; // Save file SaveData current_data = new SaveData(Chunk_Manager, player.transform.position); binary_formatter.Serialize(file_stream, current_data); file_stream.Close(); }