Created
December 10, 2015 10:56
-
-
Save ikhanhmai/79e9e99002da575659c6 to your computer and use it in GitHub Desktop.
Unity C# - Serializer Class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Runtime.Serialization.Formatters.Binary; | |
public class Serializer | |
{ | |
public static T Load<T>(string filename) where T: class | |
{ | |
if (File.Exists(filename)) | |
{ | |
try | |
{ | |
using (Stream stream = File.OpenRead(filename)) | |
{ | |
BinaryFormatter formatter = new BinaryFormatter(); | |
return formatter.Deserialize(stream) as T; | |
} | |
} | |
catch (Exception e) | |
{ | |
Debug.Log(e.Message); | |
} | |
} | |
return default(T); | |
} | |
public static void Save<T>(string filename, T data) where T: class | |
{ | |
using (Stream stream = File.OpenWrite(filename)) | |
{ | |
BinaryFormatter formatter = new BinaryFormatter(); | |
formatter.Serialize(stream, data); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment