using System; using System.IO; #if !UNITY_WSA || !UNITY_WINRT using System.Runtime.Serialization.Formatters.Binary; #endif using System.Text; using UnityEngine; namespace BayatGames.SaveGameFree.Serializers { /// /// Save Game Binary Serializer. /// public class SaveGameBinarySerializer : ISaveGameSerializer { /// /// Serialize the specified object to stream with encoding. /// /// Object. /// Stream. /// Encoding. /// The 1st type parameter. public void Serialize ( T obj, Stream stream, Encoding encoding ) { #if !UNITY_WSA || !UNITY_WINRT try { BinaryFormatter formatter = new BinaryFormatter (); formatter.Serialize ( stream, obj ); } catch ( Exception ex ) { Debug.LogException ( ex ); } #else Debug.LogError ( "SaveGameFree: The Binary Serialization isn't supported in Windows Store and UWP." ); #endif } /// /// Deserialize the specified object from stream using the encoding. /// /// Stream. /// Encoding. /// The 1st type parameter. public T Deserialize ( Stream stream, Encoding encoding ) { T result = default(T); #if !UNITY_WSA || !UNITY_WINRT try { BinaryFormatter formatter = new BinaryFormatter (); result = ( T )formatter.Deserialize ( stream ); } catch ( Exception ex ) { Debug.LogException ( ex ); } #else Debug.LogError ( "SaveGameFree: The Binary Serialization isn't supported in Windows Store and UWP." ); #endif return result; } } }