Skip to content

Instantly share code, notes, and snippets.

@GregLukosek
Last active September 1, 2016 14:22
Show Gist options
  • Save GregLukosek/52a068f9a81724e2570cb36926feff74 to your computer and use it in GitHub Desktop.
Save GregLukosek/52a068f9a81724e2570cb36926feff74 to your computer and use it in GitHub Desktop.
Get bounds of Unity Renderer and all its children
public Bounds GetCombinedBounds(GameObject parent) {
Bounds combinedBounds = new Bounds();
//grab all child renderers
Renderer[] renderers = GetComponentsInChildren<Renderer>(GetComponent<Renderer>());
//grow combined bounds with every children renderer
foreach (Renderer rendererChild in renderers) {
if (combinedBounds.size == Vector3.zero) {
combinedBounds = rendererChild.bounds;
}
combinedBounds.Encapsulate(rendererChild.bounds);
}
//at this point combinedBounds should be size of renderer and all its renderers children
return combinedBounds;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment