Created
March 30, 2019 13:34
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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