-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitySerializedDictionary.cs
More file actions
35 lines (30 loc) · 1.04 KB
/
UnitySerializedDictionary.cs
File metadata and controls
35 lines (30 loc) · 1.04 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
using System.Collections.Generic;
using UnityEngine;
namespace Demos.SOArchApproach.CodeBase.ScriptableObjectsFramework
{
public abstract class UnitySerializedDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver
{
[SerializeField, HideInInspector]
private List<TKey> keyData = new List<TKey>();
[SerializeField, HideInInspector]
private List<TValue> valueData = new List<TValue>();
void ISerializationCallbackReceiver.OnAfterDeserialize()
{
this.Clear();
for(int i = 0; i < this.keyData.Count && i < this.valueData.Count; i++)
{
this[this.keyData[i]] = this.valueData[i];
}
}
void ISerializationCallbackReceiver.OnBeforeSerialize()
{
this.keyData.Clear();
this.valueData.Clear();
foreach(var item in this)
{
this.keyData.Add(item.Key);
this.valueData.Add(item.Value);
}
}
}
}