-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathSerializedInstance.cs
128 lines (102 loc) · 2.83 KB
/
SerializedInstance.cs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using UnityEditor;
namespace UnityEngine.AssetGraph {
[System.Serializable]
public class SerializedInstance<T> : ISerializationCallbackReceiver where T: class {
[SerializeField] private string m_className;
[SerializeField] private string m_instanceData;
private T m_object;
public string ClassName {
get {
return m_className;
}
}
public T Object {
get {
if(m_object == null) {
m_object = Deserialize();
}
return m_object;
}
}
public string Data {
get {
return m_instanceData;
}
}
public SerializedInstance() {
m_className = string.Empty;
m_instanceData = string.Empty;
}
public SerializedInstance(SerializedInstance<T> instance) {
m_className = instance.m_className;
m_instanceData = instance.m_instanceData;
}
public SerializedInstance(T obj) {
UnityEngine.Assertions.Assert.IsNotNull(obj);
m_className = obj.GetType().AssemblyQualifiedName;
m_instanceData = CustomScriptUtility.EncodeString(EditorJsonUtility.ToJson(obj));
}
public void OnBeforeSerialize()
{
Save();
}
public void OnAfterDeserialize()
{
m_className = VersionCompatibilityUtility.UpdateClassName(m_className);
}
public void Invalidate() {
m_object = null;
}
private T Deserialize() {
Type instanceType = null;
if(!string.IsNullOrEmpty(m_className)) {
instanceType = Type.GetType(m_className);
}
if(!string.IsNullOrEmpty(m_instanceData) && instanceType != null) {
string data = CustomScriptUtility.DecodeString(m_instanceData);
var instance = System.Activator.CreateInstance(instanceType) as T;
EditorJsonUtility.FromJsonOverwrite(data, instance);
return instance;
}
return default(T);
}
public void Save() {
if(m_object != null) {
m_className = m_object.GetType().AssemblyQualifiedName;
m_instanceData = CustomScriptUtility.EncodeString(EditorJsonUtility.ToJson(m_object));
}
}
public T Clone() {
Save();
return Deserialize();
}
public override bool Equals(object rhs)
{
SerializedInstance<T> other = rhs as SerializedInstance<T>;
if (other == null) {
return false;
} else {
return other == this;
}
}
public override int GetHashCode()
{
return (m_instanceData == null)? base.GetHashCode() : m_instanceData.GetHashCode();
}
public static bool operator == (SerializedInstance<T> lhs, SerializedInstance<T> rhs) {
object lobj = lhs;
object robj = rhs;
if(lobj == null && robj == null) {
return true;
}
if(lobj == null || robj == null) {
return false;
}
return lhs.m_className != rhs.m_className && lhs.m_instanceData == rhs.m_instanceData;
}
public static bool operator != (SerializedInstance<T> lhs, SerializedInstance<T> rhs) {
return !(lhs == rhs);
}
}
}