Last active
September 1, 2016 14:22
-
-
Save GregLukosek/52a068f9a81724e2570cb36926feff74 to your computer and use it in GitHub Desktop.
Get bounds of Unity Renderer and all its children
This file contains hidden or 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
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