Skip to content

Instantly share code, notes, and snippets.

@danielbierwirth
Last active February 23, 2019 13:34
Show Gist options
  • Save danielbierwirth/2634556e948959db6b41ecda4662b608 to your computer and use it in GitHub Desktop.
Save danielbierwirth/2634556e948959db6b41ecda4662b608 to your computer and use it in GitHub Desktop.
Quickly access Child Components
using System.Collections.Generic;
namespace UnityEngine
{
public static class UnityEngineEx
{
public static T GetComponentInDirectChildren<T>(this Component parent) where T : Component
{
return parent.GetComponentInDirectChildren<T>(false);
}
public static T GetComponentInDirectChildren<T>(this Component parent, bool includeInactive) where T : Component
{
foreach (Transform transform in parent.transform)
{
if (includeInactive || transform.gameObject.activeInHierarchy)
{
T component = transform.GetComponent<T>();
if (component != null)
{
return component;
}
}
}
return null;
}
public static T[] GetComponentsInDirectChildren<T>(this Component parent) where T : Component
{
return parent.GetComponentsInDirectChildren<T>(false);
}
public static T[] GetComponentsInDirectChildren<T>(this Component parent, bool includeInactive) where T : Component
{
List<T> tmpList = new List<T>();
foreach (Transform transform in parent.transform)
{
if (includeInactive || transform.gameObject.activeInHierarchy)
{
tmpList.AddRange(transform.GetComponents<T>());
}
}
return tmpList.ToArray();
}
public static T GetComponentInSiblings<T>(this Component sibling) where T : Component
{
return sibling.GetComponentInSiblings<T>(false);
}
public static T GetComponentInSiblings<T>(this Component sibling, bool includeInactive) where T : Component
{
Transform parent = sibling.transform.parent;
if (parent == null) return null;
foreach (Transform transform in parent)
{
if (includeInactive || transform.gameObject.activeInHierarchy)
{
if (transform != sibling)
{
T component = transform.GetComponent<T>();
if (component != null)
{
return component;
}
}
}
}
return null;
}
public static T[] GetComponentsInSiblings<T>(this Component sibling) where T : Component
{
return sibling.GetComponentsInSiblings<T>(false);
}
public static T[] GetComponentsInSiblings<T>(this Component sibling, bool includeInactive) where T : Component
{
Transform parent = sibling.transform.parent;
if (parent == null) return null;
List<T> tmpList = new List<T>();
foreach (Transform transform in parent)
{
if (includeInactive || transform.gameObject.activeInHierarchy)
{
if (transform != sibling)
{
tmpList.AddRange(transform.GetComponents<T>());
}
}
}
return tmpList.ToArray();
}
public static T GetComponentInDirectParent<T>(this Component child) where T : Component
{
Transform parent = child.transform.parent;
if (parent == null) return null;
return parent.GetComponent<T>();
}
public static T[] GetComponentsInDirectParent<T>(this Component child) where T : Component
{
Transform parent = child.transform.parent;
if (parent == null) return null;
return parent.GetComponents<T>();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment