Skip to content

Instantly share code, notes, and snippets.

@BlueBreathHI
Last active June 5, 2019 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlueBreathHI/373105b83008b28c00589ae5881d9542 to your computer and use it in GitHub Desktop.
Save BlueBreathHI/373105b83008b28c00589ae5881d9542 to your computer and use it in GitHub Desktop.
Get all child objects. (TestFile use OdinInspector)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public static class GetAllChildren_Object{
public static GameObject[] GetAllChildren(this GameObject thisObj,bool getInvalid = false){
return thisObj.GetComponentsInChildren<Transform>(getInvalid).Where(com => com != thisObj.transform).Select(com => com.gameObject).ToArray();
}
}
public static class GetAllChildren_Component{
public static GameObject[] GetAllChildren(this Component thisCom,bool getInvalid = false){
return thisCom.GetComponentsInChildren<Transform>(getInvalid).Where(com => com != thisCom.transform).Select(com => com.gameObject).ToArray();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using System.Linq;
public class GetAllChildrenTest : MonoBehaviour
{
[Button]
void GetAllChildren_Test(){
GameObject[] children = gameObject.GetAllChildren();
children.ToList().ForEach(x=>Debug.Log("Child:"+ x.name));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment