Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- [Serializable]
- public class SerializableDictionary<T1, T2> : Dictionary<T1, T2>, ISerializationCallbackReceiver
- {
- [SerializeField] private List<T1> keys = new List<T1>();
- [SerializeField] private List<T2> values = new List<T2>();
- public void OnBeforeSerialize()
- {
- keys.Clear();
- values.Clear();
- foreach (KeyValuePair<T1, T2> pair in this)
- {
- keys.Add(pair.Key);
- values.Add(pair.Value);
- }
- }
- public void OnAfterDeserialize()
- {
- Clear();
- if (keys.Count != values.Count)
- {
- throw new Exception("there are " + keys.Count + " keys and " + values.Count + " values after deserialization. Make sure that both key and value types are serializable.");
- }
- for (int i = 0; i < keys.Count; ++i)
- {
- Add(keys[i], values[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement