-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSerializableDictionary.cs
46 lines (38 loc) · 1.17 KB
/
SerializableDictionary.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
using System;
using System.Collections.Generic;
namespace UnityEngine
{
[Serializable]
public abstract class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
{
[SerializeField]
private TKey[] keys = new TKey[0];
[SerializeField]
private TValue[] values = new TValue[0];
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
Clear();
for (var i = 0; i < this.keys.Length; i++)
{
var value = (i >= this.values.Length) ? default : this.values[i];
Add(this.keys[i], value);
}
this.keys = new TKey[0];
this.values = new TValue[0];
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
if (this.Count <= 0)
return;
this.keys = new TKey[this.Count];
this.values = new TValue[this.Count];
var i = 0;
foreach (var kv in this)
{
this.keys[i] = kv.Key;
this.values[i] = kv.Value;
i += 1;
}
}
}
}