Skip to content

Instantly share code, notes, and snippets.

@FreyaHolmer
Last active September 29, 2015 09:22
Show Gist options
  • Save FreyaHolmer/7d7e920b95723f89fc3b to your computer and use it in GitHub Desktop.
Save FreyaHolmer/7d7e920b95723f89fc3b to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections.Generic;
public static class GameObjectExtensions {
public static T[] GetComponentsInChildrenOfAsset<T>( this GameObject go ) where T : Component {
List<Transform> tfs = new List<Transform>();
CollectChildren( tfs, go.transform );
List<T> all = new List<T>();
for (int i = 0; i < tfs.Count; i++)
all.AddRange( tfs[i].gameObject.GetComponents<T>() );
return all.ToArray();
}
static void CollectChildren( List<Transform> transforms, Transform tf){
transforms.Add(tf);
foreach(Transform child in tf){
CollectChildren(transforms, child);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment