Skip to content

Instantly share code, notes, and snippets.

@aviadmini
Created March 30, 2019 13:34
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 aviadmini/c962448a301edb315a7308f43afddfb8 to your computer and use it in GitHub Desktop.
Save aviadmini/c962448a301edb315a7308f43afddfb8 to your computer and use it in GitHub Desktop.
[Unity3D] Editor script that counts object count of all children of selected prefab/game object
using System.Linq;
using UnityEditor;
using UnityEngine;
public class ObjectDeepCount : MonoBehaviour {
[MenuItem("Assets/Count objects")]
private static void DeepCountObjectHierarchy() {
foreach (GameObject o in Selection.objects.OfType<GameObject>()) {
Debug.Log(string.Format("Children count: {0}", CountChildren(o.transform)));
}
}
public static int CountChildren(Transform go) {
int count = 0;
foreach (Transform child in go.transform) {
count += CountChildren(child); // add the number of children the child has to total
++count; // add the child itself to total
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment