Mercurial > hg > Members > kazuma > JungleforUnity
diff Assets/Application/Scripts/ConvertObject.cs @ 2:ca28bf83fc89
Add ConvertObject
author | Kazuma |
---|---|
date | Mon, 07 Nov 2016 02:05:00 +0900 |
parents | |
children | 2878be4487ec |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Assets/Application/Scripts/ConvertObject.cs Mon Nov 07 02:05:00 2016 +0900 @@ -0,0 +1,26 @@ +using UnityEngine; +using System.Collections; +using System; +using System.Runtime.Serialization.Formatters.Binary; +using System.IO; +public class ConvertObject : MonoBehaviour { + + public static byte[] Convert (object target) { + BinaryFormatter bf = new BinaryFormatter(); + using (var ms = new MemoryStream()) + { + bf.Serialize(ms, target); + return ms.ToArray(); + } + } + + 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); + return binForm.Deserialize(memStream) as object; + } + } +}