#if NET_4_6 || NET_STANDARD_2_0 using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization; using UnityEngine; using System; namespace RocketUtils.SerializableDictionary { public abstract class SerializableHashSetBase { public abstract class Storage {} protected class HashSet : System.Collections.Generic.HashSet { public HashSet() {} public HashSet(ISet set) : base(set) {} public HashSet(SerializationInfo info, StreamingContext context) : base(info, context) {} } } [Serializable] public abstract class SerializableHashSet : SerializableHashSetBase, ISet, ISerializationCallbackReceiver, IDeserializationCallback, ISerializable { HashSet m_hashSet; [SerializeField] T[] m_keys; public SerializableHashSet() { m_hashSet = new HashSet(); } public SerializableHashSet(ISet set) { m_hashSet = new HashSet(set); } public void CopyFrom(ISet set) { m_hashSet.Clear(); foreach (var value in set) { m_hashSet.Add(value); } } public void OnAfterDeserialize() { if(m_keys != null) { m_hashSet.Clear(); int n = m_keys.Length; for(int i = 0; i < n; ++i) { m_hashSet.Add(m_keys[i]); } m_keys = null; } } public void OnBeforeSerialize() { int n = m_hashSet.Count; m_keys = new T[n]; int i = 0; foreach(var value in m_hashSet) { m_keys[i] = value; ++i; } } #region ISet public int Count { get { return ((ISet)m_hashSet).Count; } } public bool IsReadOnly { get { return ((ISet)m_hashSet).IsReadOnly; } } public bool Add(T item) { return ((ISet)m_hashSet).Add(item); } public void ExceptWith(IEnumerable other) { ((ISet)m_hashSet).ExceptWith(other); } public void IntersectWith(IEnumerable other) { ((ISet)m_hashSet).IntersectWith(other); } public bool IsProperSubsetOf(IEnumerable other) { return ((ISet)m_hashSet).IsProperSubsetOf(other); } public bool IsProperSupersetOf(IEnumerable other) { return ((ISet)m_hashSet).IsProperSupersetOf(other); } public bool IsSubsetOf(IEnumerable other) { return ((ISet)m_hashSet).IsSubsetOf(other); } public bool IsSupersetOf(IEnumerable other) { return ((ISet)m_hashSet).IsSupersetOf(other); } public bool Overlaps(IEnumerable other) { return ((ISet)m_hashSet).Overlaps(other); } public bool SetEquals(IEnumerable other) { return ((ISet)m_hashSet).SetEquals(other); } public void SymmetricExceptWith(IEnumerable other) { ((ISet)m_hashSet).SymmetricExceptWith(other); } public void UnionWith(IEnumerable other) { ((ISet)m_hashSet).UnionWith(other); } void ICollection.Add(T item) { ((ISet)m_hashSet).Add(item); } public void Clear() { ((ISet)m_hashSet).Clear(); } public bool Contains(T item) { return ((ISet)m_hashSet).Contains(item); } public void CopyTo(T[] array, int arrayIndex) { ((ISet)m_hashSet).CopyTo(array, arrayIndex); } public bool Remove(T item) { return ((ISet)m_hashSet).Remove(item); } public IEnumerator GetEnumerator() { return ((ISet)m_hashSet).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return ((ISet)m_hashSet).GetEnumerator(); } #endregion #region IDeserializationCallback public void OnDeserialization(object sender) { ((IDeserializationCallback)m_hashSet).OnDeserialization(sender); } #endregion #region ISerializable protected SerializableHashSet(SerializationInfo info, StreamingContext context) { m_hashSet = new HashSet(info, context); } public void GetObjectData(SerializationInfo info, StreamingContext context) { ((ISerializable)m_hashSet).GetObjectData(info, context); } #endregion } } #endif