using System.Collections.Generic;
using UnityEngine;
namespace FirstGearGames.Utilities.Objects
{
public static class Transforms
{
///
/// Returns the topmost parent for a transform.
///
///
///
public static Transform TopmostParent(this Transform t)
{
if (t.parent == null)
{
return t;
}
else
{
Transform result = t.parent;
while (result.parent != null)
result = result.parent;
return result;
}
}
///
/// Destroys all children under the specified transform.
///
///
public static void DestroyChildren(this Transform t, bool destroyImmediately = false)
{
foreach (Transform child in t)
{
if (destroyImmediately)
MonoBehaviour.DestroyImmediate(child.gameObject);
else
MonoBehaviour.Destroy(child.gameObject);
}
}
///
/// Gets components in children and optionally parent.
///
///
///
///
///
///
public static void GetComponentsInChildren(Transform parent, List results, bool includeParent = true, bool includeInactive = false) where T : Component
{
if (!includeParent)
{
List current = new List();
for (int i = 0; i < parent.childCount; i++)
{
parent.GetChild(i).GetComponentsInChildren(includeInactive, current);
results.AddRange(current);
}
}
else
{
parent.GetComponentsInChildren(includeInactive, results);
}
}
}
}