Mercurial > hg > Members > kazuma > JungleforUnity
comparison Assets/Application/Scripts/ConvertObject.cs @ 2:ca28bf83fc89
Add ConvertObject
author | Kazuma |
---|---|
date | Mon, 07 Nov 2016 02:05:00 +0900 |
parents | |
children | 2878be4487ec |
comparison
equal
deleted
inserted
replaced
1:ec859f6ce63b | 2:ca28bf83fc89 |
---|---|
1 using UnityEngine; | |
2 using System.Collections; | |
3 using System; | |
4 using System.Runtime.Serialization.Formatters.Binary; | |
5 using System.IO; | |
6 public class ConvertObject : MonoBehaviour { | |
7 | |
8 public static byte[] Convert (object target) { | |
9 BinaryFormatter bf = new BinaryFormatter(); | |
10 using (var ms = new MemoryStream()) | |
11 { | |
12 bf.Serialize(ms, target); | |
13 return ms.ToArray(); | |
14 } | |
15 } | |
16 | |
17 public static object UnConvert(byte[] target) { | |
18 using (var memStream = new MemoryStream()) | |
19 { | |
20 var binForm = new BinaryFormatter(); | |
21 memStream.Write(target, 0, target.Length); | |
22 memStream.Seek(0, SeekOrigin.Begin); | |
23 return binForm.Deserialize(memStream) as object; | |
24 } | |
25 } | |
26 } |