using System; using System.Collections.Generic; namespace UnityEngine.Rendering { // // Unity can't serialize Dictionary so here's a custom wrapper that does. Note that you have to // extend it before it can be serialized as Unity won't serialized generic-based types either. // // Example: // public sealed class MyDictionary : SerializedDictionary {} // /// /// Serialized Dictionary /// /// Key Type /// Value Type [Serializable] public class SerializedDictionary : SerializedDictionary { public override K SerializeKey(K key) => key; public override V SerializeValue(V val) => val; public override K DeserializeKey(K key) => key; public override V DeserializeValue(V val) => val; } [Serializable] public abstract class SerializedDictionary : Dictionary, ISerializationCallbackReceiver { [SerializeField] List m_Keys = new List(); [SerializeField] List m_Values = new List(); /// /// Serialize key K to SK /// public abstract SK SerializeKey(K key); /// /// Serialize value V to SV /// public abstract SV SerializeValue(V value); /// /// Deserialize key SK to K /// public abstract K DeserializeKey(SK serializedKey); /// /// Deserialize value SV to V /// public abstract V DeserializeValue(SV serializedValue); /// /// OnBeforeSerialize implementation. /// public void OnBeforeSerialize() { m_Keys.Clear(); m_Values.Clear(); foreach (var kvp in this) { m_Keys.Add(SerializeKey(kvp.Key)); m_Values.Add(SerializeValue(kvp.Value)); } } /// /// OnAfterDeserialize implementation. /// public void OnAfterDeserialize() { for (int i = 0; i < m_Keys.Count; i++) Add(DeserializeKey(m_Keys[i]), DeserializeValue(m_Values[i])); m_Keys.Clear(); m_Values.Clear(); } } }