view Assets/Application/Scripts/Module/ConvertObject.cs @ 5:12f4f937da7f

Add BenchMark
author Kazuma
date Thu, 10 Nov 2016 04:21:19 +0900
parents
children
line wrap: on
line source

using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.IO;
[Serializable]
public class ConvertObject : MonoBehaviour {

	public static byte[] Convert (object target) {
		IFormatter formatter = new BinaryFormatter();
		MemoryStream ms = new MemoryStream();
		formatter.Serialize(ms, target);
		byte[] myByteArray = ms.ToArray();
		return myByteArray;
	}

	public static object UnConvert(byte[] target) {
		using (var memStream = new MemoryStream())
		{
			var binForm = new BinaryFormatter();
			memStream.Write(target, 0, target.Length);
			memStream.Seek(0, SeekOrigin.Begin);
			var obj = binForm.Deserialize(memStream);
			return obj;
		}
	}
}