Skip to content

Instantly share code, notes, and snippets.

@develax
Created October 2, 2019 18:28
Show Gist options
  • Save develax/045fcd8631d44466af2c513dc8121b63 to your computer and use it in GitHub Desktop.
Save develax/045fcd8631d44466af2c513dc8121b63 to your computer and use it in GitHub Desktop.
Serialize To By Array
using UnityEngine;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace Utils
{
//Extension class to provide serialize / deserialize methods to object.
//src: http://stackoverflow.com/questions/1446547/how-to-convert-an-object-to-a-byte-array-in-c-sharp
//NOTE: You need add [Serializable] attribute in your class to enable serialization
public static class ObjectSerializationExtension
{
public static byte[] SerializeToByteArray(this object obj)
{
if (obj == null)
{
return null;
}
var bf = new BinaryFormatter();
using (var ms = new MemoryStream())
{
bf.Serialize(ms, obj);
return ms.ToArray();
}
}
public static T Deserialize<T>(this byte[] byteArray) where T : class
{
if (byteArray == null)
{
return null;
}
using (var memStream = new MemoryStream())
{
var binForm = new BinaryFormatter();
memStream.Write(byteArray, 0, byteArray.Length);
memStream.Seek(0, SeekOrigin.Begin);
var obj = (T)binForm.Deserialize(memStream);
return obj;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment