Skip to content

Instantly share code, notes, and snippets.

@ikhanhmai
Created December 10, 2015 10:56
Show Gist options
  • Save ikhanhmai/79e9e99002da575659c6 to your computer and use it in GitHub Desktop.
Save ikhanhmai/79e9e99002da575659c6 to your computer and use it in GitHub Desktop.
Unity C# - Serializer Class
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