using System.Collections.Generic; using UnityEngine; using System.IO; using System.Xml.Serialization;
publicclassResourceTest : MonoBehaviour { // Start is called before the first frame update voidStart() { //... DeserializeTest(); }
//... voidDeserializeTest() { var test = XmlDeSerilize<TestSerialize>(Application.dataPath + "/test.xml"); Debug.Log(test.Id + " " + test.Name); } T XmlDeSerilize<T>(string path) where T : class { using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { XmlSerializer xs = new XmlSerializer(typeof(T)); var result = xs.Deserialize(fs) as T; return result; } } }
using System.Collections.Generic; using UnityEngine; using System.IO; using System.Xml.Serialization; using System.Runtime.Serialization.Formatters.Binary;
publicclassResourceTest : MonoBehaviour { // Start is called before the first frame update voidStart() { SerializeTest(); }
voidSerializeTest() { TestSerialize testSerialize = new TestSerialize { Id = 1, Name = "test", Items = new List<int>(3) { 1, 2, 2 } };
using System.Collections.Generic; using UnityEngine; using System.IO; using System.Xml.Serialization; using System.Runtime.Serialization.Formatters.Binary;
publicclassResourceTest : MonoBehaviour {
voidStart() { DeserializeTest(); }
voidDeserializeTest() { var test = BinaryDeserialize(); Debug.Log(test.Id + " " + test.Name); }
TestSerialize BinaryDeserialize() { TextAsset textAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/test.bytes"); using(MemoryStream ms = new MemoryStream(textAsset.bytes)) { BinaryFormatter bf = new BinaryFormatter(); TestSerialize result = (TestSerialize)bf.Deserialize(ms); return result; } } }