comparison Assets/Application/Scripts/ConvertObject.cs @ 4:2878be4487ec

add Maping Code.
author Kazuma
date Tue, 08 Nov 2016 17:07:48 +0900
parents ca28bf83fc89
children
comparison
equal deleted inserted replaced
3:2dd40b4412e4 4:2878be4487ec
1 using UnityEngine; 1 using UnityEngine;
2 using System.Collections; 2 using System.Collections;
3 using System; 3 using System;
4 using System.Runtime.Serialization.Formatters.Binary; 4 using System.Runtime.Serialization.Formatters.Binary;
5 using System.IO; 5 using System.IO;
6 [Serializable]
6 public class ConvertObject : MonoBehaviour { 7 public class ConvertObject : MonoBehaviour {
7 8
8 public static byte[] Convert (object target) { 9 public static byte[] Convert (object target) {
9 BinaryFormatter bf = new BinaryFormatter(); 10 BinaryFormatter bf = new BinaryFormatter();
10 using (var ms = new MemoryStream()) 11 MemoryStream ms = new MemoryStream();
11 { 12 bf.Serialize(ms, target);
12 bf.Serialize(ms, target); 13 byte[] myByteArray = ms.ToArray();
13 return ms.ToArray(); 14 return myByteArray;
14 }
15 } 15 }
16 16
17 public static object UnConvert(byte[] target) { 17 public static object UnConvert(byte[] target) {
18 using (var memStream = new MemoryStream()) 18 using (var memStream = new MemoryStream())
19 { 19 {
20 var binForm = new BinaryFormatter(); 20 var binForm = new BinaryFormatter();
21 memStream.Write(target, 0, target.Length); 21 memStream.Write(target, 0, target.Length);
22 memStream.Seek(0, SeekOrigin.Begin); 22 memStream.Seek(0, SeekOrigin.Begin);
23 return binForm.Deserialize(memStream) as object; 23 var obj = binForm.Deserialize(memStream);
24 return obj;
24 } 25 }
25 } 26 }
26 } 27 }