using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using Object = UnityEngine.Object; // VolFx © NullTale - https://x.com/NullTale namespace VolFx.Tools { [Serializable] public class SoCollection : IReadOnlyDictionary, IList where T : ScriptableObject { [SerializeField] private List m_List = new List(); public T this[string key] => m_List.FirstOrDefault(n => n.name == key); public T this[int index] { get => m_List[index]; set => m_List[index] = value; } public IEnumerable Keys => m_List.Select(n => n.name); public IEnumerable Values => m_List; public int Count => m_List.Count; public bool IsReadOnly => false; // ======================================================================= private class EnumeratorWrapper : IEnumerator> { private IEnumerator m_Source; public KeyValuePair Current => new KeyValuePair(m_Source.Current.name, m_Source.Current); object IEnumerator. Current => Current; // ======================================================================= public EnumeratorWrapper(IEnumerable source) { m_Source = source.GetEnumerator(); } public bool MoveNext() { return m_Source.MoveNext(); } public void Reset() { } public void Dispose() { m_Source.Dispose(); } } // ======================================================================= public void Add(T item) { m_List.Add(item); } public void Clear() { m_List.Clear(); } public bool Contains(T item) { return m_List.Contains(item); } public void CopyTo(T[] array, int arrayIndex) { m_List.CopyTo(array, arrayIndex); } public bool Remove(T item) { return m_List.Remove(item); } public int IndexOf(T item) { return m_List.IndexOf(item); } public void Insert(int index, T item) { m_List.Insert(index, item); } public void RemoveAt(int index) { m_List.RemoveAt(index); } IEnumerator> IEnumerable>.GetEnumerator() { return new EnumeratorWrapper(m_List); } public IEnumerator GetEnumerator() { return m_List.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public bool ContainsKey(string key) { return m_List.Any(n => n.name == key); } public bool TryGetValue(string key, out T value) { var val = m_List.FirstOrDefault(n => n.name == key); if (val == null) { value = null; return false; } value = val; return true; } // ======================================================================= public void Destroy() { #if UNITY_EDITOR foreach (var obj in m_List) { UnityEditor.AssetDatabase.RemoveObjectFromAsset(obj); Object.DestroyImmediate(obj); } m_List.Clear(); UnityEditor.AssetDatabase.SaveAssets(); #endif } } }