public static BinaryFormatter GetBinaryFormatter() { BinaryFormatter formatter = new BinaryFormatter(); SurrogateSelector selector = new SurrogateSelector(); Vector3SerializationSurrogate vector3Surrogate = new Vector3SerializationSurrogate(); QuarternionSerializationSurrogate quaternionSurrogate = new QuarternionSerializationSurrogate(); selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3Surrogate); selector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), quaternionSurrogate); formatter.SurrogateSelector = selector; return(formatter); }
public void Save(int score, Vector3 playerPos, Vector3 enemyPos) { BinaryFormatter bf = new BinaryFormatter(); FileStream fs = File.Open(Application.persistentDataPath + "/GameData.dat", FileMode.OpenOrCreate); GameData data = new GameData(); SurrogateSelector ss = new SurrogateSelector(); Vector3SerializationSurrogate v3ss = new Vector3SerializationSurrogate(); ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); data.score = score; data.playerPos = playerPos; data.enemyPos = enemyPos; bf.Serialize(fs, data); fs.Close(); }
private static BinaryFormatter CreateBinaryFormatter() { BinaryFormatter bf = new BinaryFormatter(); SurrogateSelector ss = new SurrogateSelector(); Vector3SerializationSurrogate v3ss = new Vector3SerializationSurrogate(); ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); // 5. Have the formatter use our surrogate selector bf.SurrogateSelector = ss; return(new BinaryFormatter()); }
private static TData DeserializeFromString <TData>(string settings) { byte[] b = Convert.FromBase64String(settings); using (var stream = new MemoryStream(b)) { SurrogateSelector ss = new SurrogateSelector(); Vector3SerializationSurrogate v3ss = new Vector3SerializationSurrogate(); ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); var formatter = new BinaryFormatter(); formatter.SurrogateSelector = ss; stream.Seek(0, SeekOrigin.Begin); return((TData)formatter.Deserialize(stream)); } }
private static BinaryFormatter CreatePaintingBinaryFormatter() { BinaryFormatter bf = new BinaryFormatter(); SurrogateSelector ss = new SurrogateSelector(); StreamingContext sc = new StreamingContext(StreamingContextStates.All); ColorSerializationSurrogate css = new ColorSerializationSurrogate(); Vector3SerializationSurrogate vss = new Vector3SerializationSurrogate(); // any non-serializable class needs to have a serialization surrogate ss.AddSurrogate(typeof(Color), sc, css); ss.AddSurrogate(typeof(Vector3), sc, vss); bf.SurrogateSelector = ss; return(bf); }
private static string SerializeToString <TData>(TData settings) { using (var stream = new MemoryStream()) { SurrogateSelector ss = new SurrogateSelector(); Vector3SerializationSurrogate v3ss = new Vector3SerializationSurrogate(); ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); var formatter = new BinaryFormatter(); formatter.SurrogateSelector = ss; formatter.Serialize(stream, settings); stream.Flush(); stream.Position = 0; return(Convert.ToBase64String(stream.ToArray())); } }
void Serialize() { BinaryFormatter bf = new BinaryFormatter(); // 1. Construct a SurrogateSelector object SurrogateSelector ss = new SurrogateSelector(); Vector3SerializationSurrogate v3ss = new Vector3SerializationSurrogate(); ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); // 2. Have the formatter use our surrogate selector bf.SurrogateSelector = ss; }
static BinaryFormatter GetBinaryFormatter() { var formatter = new BinaryFormatter(); var selector = new SurrogateSelector(); var v3Surrogate = new Vector3SerializationSurrogate(); var qSurrogate = new QuaternionSerializationSurrogate(); var cSurrogate = new ColorSerializationSurrogate(); selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3Surrogate); selector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), qSurrogate); selector.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), cSurrogate); formatter.SurrogateSelector = selector; return(formatter); }
public static BinaryFormatter GetBinaryFormatter() { BinaryFormatter formatter = new BinaryFormatter(); SurrogateSelector selector = new SurrogateSelector(); Vector3SerializationSurrogate vector3Surrogate = new Vector3SerializationSurrogate(); QuaternionSerializationSurrogate quaternionSurrogate = new QuaternionSerializationSurrogate(); LinkedListSerializationSurrogate llSurrogate = new LinkedListSerializationSurrogate(); selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3Surrogate); selector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), quaternionSurrogate); selector.AddSurrogate(typeof(LinkedList <KeyValuePair <Cards, bool> >), new StreamingContext(StreamingContextStates.All), llSurrogate); formatter.SurrogateSelector = selector; return(formatter); }
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); }
void SaveOldStyle(FileStream fileStream, string filepath) { BinaryFormatter formatter = new BinaryFormatter(); SurrogateSelector surrogateSelector = new SurrogateSelector(); Vector3SerializationSurrogate vector3SS = new Vector3SerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SS); formatter.SurrogateSelector = surrogateSelector; try { formatter.Serialize(fileStream, this); Debug.Log("Wrote out file to " + filepath); } catch (SerializationException e) { Debug.LogError("Failed to serialize. Reason: " + e.Message); throw; } }
private static Replay LoadOldStyle(Stream file) { BinaryFormatter formatter = new BinaryFormatter(); SurrogateSelector surrogateSelector = new SurrogateSelector(); Vector3SerializationSurrogate vector3SS = new Vector3SerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SS); formatter.SurrogateSelector = surrogateSelector; try { return(formatter.Deserialize(file) as Replay); } catch (SerializationException e) { Debug.Log("Failed to deserialize. Reason: " + e.Message); throw; } }
private static string SerializeToString <TData>(TData settings) { #if USE_BINARY_FORMATTER using (var stream = new MemoryStream()) { SurrogateSelector ss = new SurrogateSelector(); Vector3SerializationSurrogate v3ss = new Vector3SerializationSurrogate(); ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); var formatter = new BinaryFormatter(); formatter.SurrogateSelector = ss; formatter.Serialize(stream, settings); stream.Flush(); stream.Position = 0; return(Convert.ToBase64String(stream.ToArray())); } #else return(JsonUtility.ToJson(settings, false)); #endif }
// 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); }
//Save spline as a binary file public void SaveInfo(string path) { BinaryFormatter bf = new BinaryFormatter(); SurrogateSelector ss = new SurrogateSelector(); Vector3SerializationSurrogate v3ss = new Vector3SerializationSurrogate(); SplineSettingsSerializationSurrogate ssss = new SplineSettingsSerializationSurrogate(); ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); ss.AddSurrogate(typeof(SplineSettings), new StreamingContext(StreamingContextStates.All), ssss); bf.SurrogateSelector = ss; FileStream file = File.Create(path); bf.Serialize(file, splines); bf.Serialize(file, connectedPoints); file.Close(); }
// 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(); }
// this saves a save game slot to disk public void SavePlayerData(int saveGameSlotNumber) { // measure performance var stopwatch = new Stopwatch(); stopwatch.Start(); // get the path to the player data file var filePath = Application.persistentDataPath + "/" + m_playerDataFileName + saveGameSlotNumber + ".bin"; try { // try to save the player data file using (var file = File.Create(filePath)) { // create the binary formatter var binaryFormatter = new BinaryFormatter(); // add support for serializing / deserializing Unity.Vector3 var surrogateSelector = new SurrogateSelector(); var vector3SerializationSurrogate = new Vector3SerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SerializationSurrogate); binaryFormatter.SurrogateSelector = surrogateSelector; // serialize and save the player data file binaryFormatter.Serialize(file, m_playerDataList[saveGameSlotNumber]); // report how long it took UnityEngine.Debug.Log("Saving the player data took " + stopwatch.ElapsedMilliseconds + " milliseconds."); } } catch (IOException exception) { // report if we got an exception UnityEngine.Debug.Log("Saving player data failed - " + exception.Message); } }
//Creates a binary formatter public static BinaryFormatter GetBinaryFormatter() { BinaryFormatter formatter = new BinaryFormatter(); //Surrogates SurrogateSelector selector = new SurrogateSelector(); //Init surrogates Vector3SerializationSurrogate vector3Surrogate = new Vector3SerializationSurrogate(); QuaternionSerializationSurrogate quaternionSurrogate = new QuaternionSerializationSurrogate(); GalaxyNodeSerializationSurrogate nodeSurrogate = new GalaxyNodeSerializationSurrogate(); FactionDataSerializationSurrogate factionSurrogate = new FactionDataSerializationSurrogate(); //Add surrogates selector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3Surrogate); selector.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), quaternionSurrogate); selector.AddSurrogate(typeof(GalaxyNode), new StreamingContext(StreamingContextStates.All), nodeSurrogate); selector.AddSurrogate(typeof(FactionData), new StreamingContext(StreamingContextStates.All), factionSurrogate); //Apply surrogates formatter.SurrogateSelector = selector; return(formatter); }
static void CreateSurrogates() { ss = new SurrogateSelector(); //VECTOR2 Vector2SerializationSurrogate v2_ss = new Vector2SerializationSurrogate(); ss.AddSurrogate(typeof(Vector2), new StreamingContext(StreamingContextStates.All), v2_ss); //VECTOR3 Vector3SerializationSurrogate v3_ss = new Vector3SerializationSurrogate(); ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3_ss); //VECTOR4 Vector4SerializationSurrogate v4_ss = new Vector4SerializationSurrogate(); ss.AddSurrogate(typeof(Vector4), new StreamingContext(StreamingContextStates.All), v4_ss); //QUATERNION QuaternionSerializationSurrogate q_ss = new QuaternionSerializationSurrogate(); ss.AddSurrogate(typeof(Quaternion), new StreamingContext(StreamingContextStates.All), q_ss); //COLOR AND COLOR32 ColorSerializationSurrogate color_ss = new ColorSerializationSurrogate(); ss.AddSurrogate(typeof(Color), new StreamingContext(StreamingContextStates.All), color_ss); ss.AddSurrogate(typeof(Color32), new StreamingContext(StreamingContextStates.All), color_ss); //TEXTURE2D Texture2DSerializationSurrogate texture_ss = new Texture2DSerializationSurrogate(); ss.AddSurrogate(typeof(Texture2D), new StreamingContext(StreamingContextStates.All), texture_ss); Texture2DCompressionType_old = Texture2DCompressionType; }
//Saving and Loading public void SaveData() { Debug.Log("PlayerInfo Saved"); PlayerStats.SaveTemp (); BinaryFormatter bf = new BinaryFormatter(); SurrogateSelector ss = new SurrogateSelector(); FileStream file = File.Create(Application.persistentDataPath + "/" + FileName + ".dat"); // Vector3SerializationSurrogate v3ss = new Vector3SerializationSurrogate(); ss.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), v3ss); bf.SurrogateSelector = ss; // PlayerData data = new PlayerData(); data.PickupCount = Pickups; data.ChestCount = ChestsOpened; data.SwitchCount = SwitchesUsed; data.DeathCount = Deaths; data.checkpoint = checkpoint; data.playerHealth = playerHealth; data.currentscene = currentscene; // bf.Serialize(file, data); file.Close(); }
// this loads the save game slots from disk void LoadPlayerDataList() { // whether or not we have found the current game var currentGameFound = false; // create the player data list m_playerDataList = new PlayerData[c_numSaveGameSlots]; // go through each save game slot for (var i = 0; i < c_numSaveGameSlots; i++) { // get the path to the player data file var filePath = Application.persistentDataPath + "/" + m_playerDataFileName + i + ".bin"; // keep track of whether or not we were able to load the player data file var loadSucceeded = false; // check if the file exists if (File.Exists(filePath)) { try { // try to load the save game file now var file = File.Open(filePath, FileMode.Open); // create the binary formatter var binaryFormatter = new BinaryFormatter(); // add support for serializing / deserializing Unity.Vector3 var surrogateSelector = new SurrogateSelector(); var vector3SerializationSurrogate = new Vector3SerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SerializationSurrogate); binaryFormatter.SurrogateSelector = surrogateSelector; // load and deserialize the player data file m_playerDataList[i] = (PlayerData)binaryFormatter.Deserialize(file); // we were able to load the save game slots from file (version checking is next) loadSucceeded = true; } catch { } } // if the player data is from an old version then we have to start over if (!loadSucceeded || !m_playerDataList[i].IsCurrentVersion()) { // debug info UnityEngine.Debug.Log("Creating and resetting player data " + i); m_playerDataList[i] = new PlayerData(); m_playerDataList[i].Reset(); } // check if this is the active save game slot if (m_playerDataList[i].m_isCurrentGame) { // yes - remember the slot number m_activeSaveGameSlotNumber = i; // point the current player data to this slot m_playerData = m_playerDataList[m_activeSaveGameSlotNumber]; // we have found the current game currentGameFound = true; } } // did we not find the current game? if (!currentGameFound) { // nope - use the first slot m_activeSaveGameSlotNumber = 0; // point the current player data to this slot m_playerData = m_playerDataList[m_activeSaveGameSlotNumber]; } // set the target save game slot number to be the same as the active one m_targetSaveGameSlotNumber = m_activeSaveGameSlotNumber; }
/// <summary> /// Saves the map content to a file. /// </summary> public void SaveMap() { if (!Directory.Exists(Application.dataPath + "/Maps/")) { Directory.CreateDirectory(Application.dataPath + "/Maps/"); } BinaryFormatter bf = new BinaryFormatter(); SurrogateSelector surrogateSelector = new SurrogateSelector(); Vector3SerializationSurrogate vector3SS = new Vector3SerializationSurrogate(); surrogateSelector.AddSurrogate(typeof(Vector3), new StreamingContext(StreamingContextStates.All), vector3SS); bf.SurrogateSelector = surrogateSelector; FileStream file = File.Create(Application.dataPath + "/Maps/" + path + ".map"); MapData mapData = new MapData(); mapData.FloorPosition = GameObject.Find("Floor").transform.position; GameObject[] temp; temp = GameObject.FindGameObjectsWithTag("Movable"); Vector3[] blockPositions = new Vector3 [temp.Length]; for (int i = 0; i < temp.Length; i++) { blockPositions [i] = temp [i].transform.position; } temp = GameObject.FindGameObjectsWithTag("ElectricDoor"); Vector3[] electricDoorPositions = new Vector3[temp.Length]; string[] electricDoorNames = new string[temp.Length]; string[][] attachedPadNames = new string[temp.Length][]; int maximumPadsAttached = 3; for (int i = 0; i < temp.Length; i++) { attachedPadNames [i] = new string [maximumPadsAttached]; electricDoorNames [i] = temp [i].name; } for (int i = 0; i < temp.Length; i++) { electricDoorPositions [i] = temp [i].transform.position; for (int j = 0; j < temp [i].GetComponent <ElectricDoor> ().attachedPads.Length; j++) { if (temp [i].GetComponent <ElectricDoor> ().attachedPads [j] != null) { attachedPadNames [i] [j] = temp [i].GetComponent <ElectricDoor> ().attachedPads [j].name; } } } temp = GameObject.FindGameObjectsWithTag("Pad"); Vector3[] padPositions = new Vector3[temp.Length]; string[] padNames = new string[temp.Length]; string[] attachedMaterialNames = new string[temp.Length]; string[] attachedDoorNames = new string[temp.Length]; for (int i = 0; i < temp.Length; i++) { attachedDoorNames [i] = temp [i].GetComponent <ElectricDoorPad> ().electricDoor.gameObject.name; attachedMaterialNames [i] = temp [i].GetComponent <Renderer> ().material.name.Replace(" (Instance)", ""); padPositions [i] = temp [i].transform.position; padNames [i] = temp [i].name; } mapData.BlockPositions = blockPositions; mapData.ElectricDoorPositions = electricDoorPositions; mapData.PadPositions = padPositions; mapData.PadNames = padNames; mapData.ElectricDoorNames = electricDoorNames; mapData.AttachedPadNames = attachedPadNames; mapData.AttachedDoorNames = attachedDoorNames; mapData.AttachedMaterialNames = attachedMaterialNames; bf.Serialize(file, mapData); file.Close(); }