#if UNITY_EDITOR using UnityEngine; using System; using System.Collections.Generic; namespace O3DWB { [Serializable] public class SerializableHashSet : ISerializationCallbackReceiver where SerializedType : class { #region Private Variables private HashSet _hashSet = new HashSet(); [SerializeField] private List _serializedList = new List(); #endregion #region Public Properties public HashSet HashSet { get { return _hashSet; } } public int Count { get { return HashSet.Count; } } #endregion #region Public Methods public void OnBeforeSerialize() { RemoveNullEntries(); _serializedList.Clear(); // Store the hash set values in the serialized list foreach (SerializedType serializedEntity in _hashSet) { _serializedList.Add(serializedEntity); } } public void OnAfterDeserialize() { // Create the hash set based on the serialized list _hashSet = new HashSet(_serializedList); _serializedList.Clear(); } public void RemoveNullEntries() { _hashSet.RemoveWhere(item => EqualityComparer.Default.Equals(item, default(SerializedType))); } public bool Contains(SerializedType entity) { return _hashSet.Contains(entity); } public void Add(SerializedType entity) { _hashSet.Add(entity); } public void Remove(SerializedType entity) { _hashSet.Remove(entity); } public void RemoveWhere(Predicate match) { _hashSet.RemoveWhere(match); } public void Clear() { _hashSet.Clear(); } public void FromEnumerable(IEnumerable entities) { _hashSet = new HashSet(entities); } #endregion } } #endif