1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
#if UNITY_EDITOR
using System;
using UnityEngine;
using XUtliPoolLib;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using UnityEditor;
using System.Runtime.Serialization.Formatters.Binary;
namespace XEditor
{
public class XDataIO<T> : XSingleton<XDataIO<T>>
{
XmlSerializer _formatter = new XmlSerializer(typeof(T));
public void SerializeData(string pathwithname, T data)
{
using (FileStream writer = new FileStream(pathwithname, FileMode.Create))
{
//using Encoding
StreamWriter sw = new StreamWriter(writer, Encoding.UTF8);
XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
//empty name spaces
xsn.Add(string.Empty, string.Empty);
_formatter.Serialize(sw, data, xsn);
AssetDatabase.Refresh();
}
}
public void SerializeData(string pathwithname, T data, Type[] types)
{
using (FileStream writer = new FileStream(pathwithname, FileMode.Create))
{
//using Encoding
StreamWriter sw = new StreamWriter(writer, Encoding.UTF8);
XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
//empty name spaces
xsn.Add(string.Empty, string.Empty);
XmlSerializer formatter = new XmlSerializer(typeof(T), types);
formatter.Serialize(sw, data, xsn);
AssetDatabase.Refresh();
}
}
public T DeserializeData(string pathwithname)
{
try
{
using (FileStream reader = new FileStream(pathwithname, FileMode.Open))
{
//IFormatter formatter = new BinaryFormatter();
System.Xml.Serialization.XmlSerializer formatter = new System.Xml.Serialization.XmlSerializer(typeof(T));
return (T)formatter.Deserialize(reader);
}
}
catch (Exception e)
{
Debug.LogError(e.Message);
return default(T);
}
}
public T DeserializeData(Stream stream)
{
try
{
//IFormatter formatter = new BinaryFormatter();
System.Xml.Serialization.XmlSerializer formatter = new System.Xml.Serialization.XmlSerializer(typeof(T));
return (T)formatter.Deserialize(stream);
}
catch (Exception e)
{
Debug.LogError(e.Message);
return default(T);
}
}
public T DeserializeData(string pathwithname, Type[] types)
{
using (FileStream reader = new FileStream(pathwithname, FileMode.Open))
{
//IFormatter formatter = new BinaryFormatter();
System.Xml.Serialization.XmlSerializer formatter = new System.Xml.Serialization.XmlSerializer(typeof(T), types);
return (T)formatter.Deserialize(reader);
}
}
}
}
#endif
|