Last active
February 23, 2023 21:35
-
-
Save bellicapax/0838bfa4cff863d07baf78644a6a6b9b to your computer and use it in GitHub Desktop.
Serializable Dictionary base for Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
namespace System.Collections.Generic | |
{ | |
[Serializable] | |
public abstract class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, ISerializationCallbackReceiver | |
{ | |
protected abstract List<SerializableKeyValuePair<TKey, TValue>> _keyValuePairs { get; set; } | |
// save the dictionary to lists | |
public void OnBeforeSerialize() | |
{ | |
_keyValuePairs.Clear(); | |
foreach (KeyValuePair<TKey, TValue> pair in this) | |
{ | |
_keyValuePairs.Add(new SerializableKeyValuePair<TKey, TValue>(pair.Key, pair.Value)); | |
} | |
} | |
// load dictionary from lists | |
public void OnAfterDeserialize() | |
{ | |
this.Clear(); | |
for (int i = 0; i < _keyValuePairs.Count; i++) | |
{ | |
this[_keyValuePairs[i].Key] = _keyValuePairs[i].Value; | |
} | |
} | |
} | |
// Based on SerializableTuple from https://github.com/neuecc/SerializableDictionary | |
[Serializable] | |
public class SerializableKeyValuePair<TKey, TValue> : IEquatable<SerializableKeyValuePair<TKey, TValue>> | |
{ | |
[SerializeField] | |
TKey _key; | |
public TKey Key { get { return _key; } } | |
[SerializeField] | |
TValue _value; | |
public TValue Value { get { return _value; } } | |
public SerializableKeyValuePair() | |
{ | |
} | |
public SerializableKeyValuePair(TKey key, TValue value) | |
{ | |
this._key = key; | |
this._value = value; | |
} | |
public bool Equals(SerializableKeyValuePair<TKey, TValue> other) | |
{ | |
var comparer1 = EqualityComparer<TKey>.Default; | |
var comparer2 = EqualityComparer<TValue>.Default; | |
return comparer1.Equals(_key, other._key) && | |
comparer2.Equals(_value, other._value); | |
} | |
public override int GetHashCode() | |
{ | |
var comparer1 = EqualityComparer<TKey>.Default; | |
var comparer2 = EqualityComparer<TValue>.Default; | |
int h0; | |
h0 = comparer1.GetHashCode(_key); | |
h0 = (h0 << 5) + h0 ^ comparer2.GetHashCode(_value); | |
return h0; | |
} | |
public override string ToString() | |
{ | |
return String.Format("(Key: {0}, Value: {1})", _key, _value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that borrowing the
System.Collections.Generic
namespace works while running in the editor, it crashes builds (at least with Unity 2017.4) with the following unhelpful log lines:Using a unique namespace (like
namespace SerializableCollections
like neuecc uses) fixes it.Otherwise, thank you for this. It has been super helpful.