No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SerializedDictionary.cs 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections.Generic;
  4. namespace UnityEngine.Rendering
  5. {
  6. internal sealed class SerializedDictionaryDebugView<K, V>
  7. {
  8. private IDictionary<K, V> dict;
  9. public SerializedDictionaryDebugView(IDictionary<K, V> dictionary)
  10. {
  11. if (dictionary == null)
  12. throw new ArgumentNullException(dictionary.ToString());
  13. this.dict = dictionary;
  14. }
  15. [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
  16. public KeyValuePair<K, V>[] Items
  17. {
  18. get
  19. {
  20. KeyValuePair<K, V>[] items = new KeyValuePair<K, V>[dict.Count];
  21. dict.CopyTo(items, 0);
  22. return items;
  23. }
  24. }
  25. }
  26. /// <summary>
  27. /// Unity can't serialize Dictionary so here's a custom wrapper that does. Note that you have to
  28. /// extend it before it can be serialized as Unity won't serialized generic-based types either.
  29. /// </summary>
  30. /// <typeparam name="K">The key type</typeparam>
  31. /// <typeparam name="V">The value</typeparam>
  32. /// <example>
  33. /// public sealed class MyDictionary : SerializedDictionary&lt;KeyType, ValueType&gt; {}
  34. /// </example>
  35. [Serializable]
  36. [DebuggerDisplay("Count = {Count}")]
  37. [DebuggerTypeProxy(typeof(SerializedDictionaryDebugView<, >))]
  38. public class SerializedDictionary<K, V> : SerializedDictionary<K, V, K, V>
  39. {
  40. /// <summary>
  41. /// Conversion to serialize a key
  42. /// </summary>
  43. /// <param name="key">The key to serialize</param>
  44. /// <returns>The Key that has been serialized</returns>
  45. public override K SerializeKey(K key) => key;
  46. /// <summary>
  47. /// Conversion to serialize a value
  48. /// </summary>
  49. /// <param name="val">The value</param>
  50. /// <returns>The value</returns>
  51. public override V SerializeValue(V val) => val;
  52. /// <summary>
  53. /// Conversion to serialize a key
  54. /// </summary>
  55. /// <param name="key">The key to serialize</param>
  56. /// <returns>The Key that has been serialized</returns>
  57. public override K DeserializeKey(K key) => key;
  58. /// <summary>
  59. /// Conversion to serialize a value
  60. /// </summary>
  61. /// <param name="val">The value</param>
  62. /// <returns>The value</returns>
  63. public override V DeserializeValue(V val) => val;
  64. }
  65. /// <summary>
  66. /// Dictionary that can serialize keys and values as other types
  67. /// </summary>
  68. /// <typeparam name="K">The key type</typeparam>
  69. /// <typeparam name="V">The value type</typeparam>
  70. /// <typeparam name="SK">The type which the key will be serialized for</typeparam>
  71. /// <typeparam name="SV">The type which the value will be serialized for</typeparam>
  72. [Serializable]
  73. public abstract class SerializedDictionary<K, V, SK, SV> : Dictionary<K, V>, ISerializationCallbackReceiver
  74. {
  75. [SerializeField]
  76. List<SK> m_Keys = new List<SK>();
  77. [SerializeField]
  78. List<SV> m_Values = new List<SV>();
  79. /// <summary>
  80. /// From <see cref="K"/> to <see cref="SK"/>
  81. /// </summary>
  82. /// <param name="key">They key in <see cref="K"/></param>
  83. /// <returns>The key in <see cref="SK"/></returns>
  84. public abstract SK SerializeKey(K key);
  85. /// <summary>
  86. /// From <see cref="V"/> to <see cref="SV"/>
  87. /// </summary>
  88. /// <param name="value">The value in <see cref="V"/></param>
  89. /// <returns>The value in <see cref="SV"/></returns>
  90. public abstract SV SerializeValue(V value);
  91. /// <summary>
  92. /// From <see cref="SK"/> to <see cref="K"/>
  93. /// </summary>
  94. /// <param name="serializedKey">They key in <see cref="SK"/></param>
  95. /// <returns>The key in <see cref="K"/></returns>
  96. public abstract K DeserializeKey(SK serializedKey);
  97. /// <summary>
  98. /// From <see cref="SV"/> to <see cref="V"/>
  99. /// </summary>
  100. /// <param name="serializedValue">The value in <see cref="SV"/></param>
  101. /// <returns>The value in <see cref="V"/></returns>
  102. public abstract V DeserializeValue(SV serializedValue);
  103. /// <summary>
  104. /// OnBeforeSerialize implementation.
  105. /// </summary>
  106. public void OnBeforeSerialize()
  107. {
  108. m_Keys.Clear();
  109. m_Values.Clear();
  110. foreach (var kvp in this)
  111. {
  112. m_Keys.Add(SerializeKey(kvp.Key));
  113. m_Values.Add(SerializeValue(kvp.Value));
  114. }
  115. }
  116. /// <summary>
  117. /// OnAfterDeserialize implementation.
  118. /// </summary>
  119. public void OnAfterDeserialize()
  120. {
  121. Clear();
  122. for (int i = 0; i < m_Keys.Count; i++)
  123. Add(DeserializeKey(m_Keys[i]), DeserializeValue(m_Values[i]));
  124. }
  125. }
  126. }