Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6.  
  7. [Serializable]
  8. public class SerializableDictionary<T1, T2> : Dictionary<T1, T2>, ISerializationCallbackReceiver
  9. {
  10. [SerializeField] private List<T1> keys = new List<T1>();
  11. [SerializeField] private List<T2> values = new List<T2>();
  12.  
  13. public void OnBeforeSerialize()
  14. {
  15. keys.Clear();
  16. values.Clear();
  17.  
  18. foreach (KeyValuePair<T1, T2> pair in this)
  19. {
  20. keys.Add(pair.Key);
  21. values.Add(pair.Value);
  22. }
  23. }
  24.  
  25. public void OnAfterDeserialize()
  26. {
  27. Clear();
  28.  
  29. if (keys.Count != values.Count)
  30. {
  31. throw new Exception("there are " + keys.Count + " keys and " + values.Count + " values after deserialization. Make sure that both key and value types are serializable.");
  32. }
  33.  
  34. for (int i = 0; i < keys.Count; ++i)
  35. {
  36. Add(keys[i], values[i]);
  37. }
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement