-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerializableDictionary.cs
More file actions
153 lines (142 loc) · 4.14 KB
/
SerializableDictionary.cs
File metadata and controls
153 lines (142 loc) · 4.14 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#define InEditorMode
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
#if InEditorMode
public class SerializableDictionary<TKey, TValue> : MonoBehaviour, ISerializationCallbackReceiver
{
[SerializeField]
private List<TKey> _keys = null;
[SerializeField]
private List<TValue> _values = null;
private Dictionary<TKey, TValue> serializedDict = null;
private FieldInfo dictField = null;
public void OnBeforeSerialize()
{
serializedDict = GetDict(GetDictField());
if (CheckDictData())
{
if (serializedDict == null)
{
_keys = null;
_values = null;
}
else
{
_keys = new List<TKey>(serializedDict.Keys);
_values = new List<TValue>(serializedDict.Values);
}
}
}
public void OnAfterDeserialize()
{
if (CheckDictData())
{
if (_keys != null && _values != null)
{
if (serializedDict == null)
{
serializedDict = new Dictionary<TKey, TValue>();
}
else
{
serializedDict.Clear();
}
for (int i = 0; i != Mathf.Min(_keys.Count, _values.Count); i++)
serializedDict.Add(_keys[i], _values[i]);
}
else
{
serializedDict = null;
}
SetDict(GetDictField(), serializedDict);
}
}
private bool CheckDictData()
{
if (_values == null && _keys == null)
{
return true;
}
if (_values == null || _keys == null)
{
return false;
}
if (_values.Count != _keys.Count)
{
Debug.LogError("Numbers of keys and values should be same!");
return false;
}
if (IsDuplicated(_keys))
{
Debug.LogError("Duplicated keys!");
return false;
}
return true;
}
private bool IsDuplicated<T>(List<T> list)
{
int len = list.Count;
for (int i = 0; i < len - 1; ++i)
{
for (int j = i + 1; j < len; ++j)
{
if (Equals(list[i], list[j]))
{
return true;
}
}
}
return false;
}
Dictionary<TKey, TValue> GetDict(FieldInfo dictField)
{
var myDict = dictField.GetValue(this);
return myDict as Dictionary<TKey, TValue>;
}
void SetDict(FieldInfo dictField, Dictionary<TKey, TValue> dict)
{
dictField.SetValue(this, dict);
}
FieldInfo GetDictField()
{
if (dictField == null)
{
System.Type type = this.GetType();
FieldInfo myDictField = null;
foreach (var field in type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance))
{
var att = field.GetCustomAttributes(false);
if (att.Length > 0 && att[0].GetType() == typeof(SerializedDict))
{
if (field.FieldType == typeof(Dictionary<TKey, TValue>))
{
myDictField = field;
break;
}
else
throw new System.Exception("Dictionary type cannot match.");
}
}
if (myDictField == null)
{
throw new System.Exception("No dictionary!");
}
else
{
dictField = myDictField;
}
}
return dictField;
}
}
[System.AttributeUsage(System.AttributeTargets.Field)]
public class SerializedDict : System.Attribute
{
}
#else
public class SerializableDictionary<TKey, TValue> : MonoBehaviour
{
}
#endif