using System.Collections; using System.Collections.Generic; using UnityEngine; namespace Unity.Tutorials.Core.Editor { /// /// Abstract base class for CollectionWrapper implementations. /// public abstract class CollectionWrapper { } /// /// Represents an abstract collection of items. The items are stored internally as List. /// Implement your own concrete collections by inheriting from this class. /// /// The type of elements in the collection. public abstract class CollectionWrapper : CollectionWrapper, IEnumerable { [SerializeField] List m_Items = new List(); /// /// Default-constructs an empty collection. /// public CollectionWrapper() {} /// /// Constructs with items. /// /// public CollectionWrapper(IList items) { SetItems(items); } /// /// Returns an item at a specific index. /// /// /// public T this[int i] { get { return m_Items[i]; } set { if ( (m_Items[i] == null && value != null) || (m_Items[i] != null && !m_Items[i].Equals(value)) ) { m_Items[i] = value; } } } /// /// The count of items. /// public int Count { get { return m_Items.Count; } } /// /// Returns an enumerator that iterates through a collection. /// /// An IEnumerator object that can be used to iterate through the collection. IEnumerator IEnumerable.GetEnumerator() { return m_Items.GetEnumerator(); } /// /// Returns an Enumerator to the items. /// /// public IEnumerator GetEnumerator() { return m_Items.GetEnumerator(); } /// /// Returns the items of the collection as a list. /// /// public void GetItems(List items) { if (items.Capacity < m_Items.Count) { items.Capacity = m_Items.Count; } items.Clear(); items.AddRange(m_Items); } /// /// Sets the items of the collection. /// /// public void SetItems(IEnumerable items) { m_Items.Clear(); m_Items.AddRange(items); } /// /// Adds an item to the collection /// /// public void AddItem(T item) { m_Items.Add(item); } } }