#if UNITY_EDITOR using UnityEngine; using UnityEditor; using System.Collections.Generic; namespace VLB { public class TargetList : IEnumerable where T : MonoBehaviour { public int Count { get { return m_Targets != null ? m_Targets.Count : 0; } } public T this[int key] { get { return m_Targets != null ? m_Targets[key] : null; } } public List m_Targets = null; public TargetList(UnityEngine.Object[] entities) { m_Targets = new List(); foreach (var entity in entities) { if (entity is T) { m_Targets.Add(entity as T); // directly get the component from the object } else { // otherwise get access from the current MonoBehaviour, in case the current MonoBehaviour is not the type wanted as target var behaviour = entity as MonoBehaviour; var comp = behaviour.GetComponent(); if (comp) m_Targets.Add(comp); } } Debug.Assert(m_Targets.Count > 0); } public bool HasAtLeastOneTargetWith(System.Func lambda) { foreach (var target in m_Targets) { if (lambda(target)) { return true; } } return false; } public bool HaveAll(System.Func lambda) { if (m_Targets.Count == 0) return false; foreach (var target in m_Targets) { if (!lambda(target)) { return false; } } return true; } public void RecordUndoAction(string name, System.Action lambda) { Undo.RecordObjects(m_Targets.ToArray(), name); foreach (var target in m_Targets) { lambda(target); } } // make this object foreach compatible public IEnumerator GetEnumerator() { foreach (var target in m_Targets) yield return target; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return this.GetEnumerator(); } } } #endif