TyrannicGoat

TransformAsList

Feb 4th, 2021
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1.  public class TransformAsList<T> : IList<T> where T : Component
  2.     {
  3.         private readonly Transform transform;
  4.         public TransformAsList(Transform transform1) => this.transform = transform1;
  5.  
  6.         public IEnumerator<T> GetEnumerator()
  7.         {
  8.             for (int i = 0; i < transform.childCount; i++)
  9.                 if (this[i] != null)
  10.                     yield return this[i];
  11.         }
  12.        
  13.         IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  14.        
  15.         public virtual void Add(T item) => item.transform.parent = transform;
  16.        
  17.         public virtual void Clear() { for (int i = 0; i < transform.childCount; i++)  RemoveAt(i); }
  18.        
  19.         public bool Contains(T item) => transform.GetComponentsInChildren<T>().Contains(item);
  20.        
  21.         public void CopyTo(T[] array, int arrayIndex) => throw new System.NotImplementedException();
  22.        
  23.         public virtual bool Remove(T item) => (item.transform.parent = null) == null;
  24.        
  25.         public int Count => transform.childCount;
  26.        
  27.         public bool IsReadOnly => false;
  28.        
  29.         public int IndexOf(T item) => item.transform.GetSiblingIndex();
  30.        
  31.         public void Insert(int index, T item)
  32.         {
  33.             item.transform.parent = transform;
  34.             item.transform.SetSiblingIndex(index);
  35.         }
  36.        
  37.         public void RemoveAt(int index) => this[index].transform.parent = null;
  38.        
  39.         public T this[int index]
  40.         {
  41.             get => transform.GetChild(index).GetComponent<T>();
  42.             set => throw new System.NotImplementedException();
  43.         }
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment