- Home /
How to determine bounding box of scene?
My objective is to determine the total dimensions of a scene.
Will I need to write a script to iterate through each object in the scene and continue adding each to an iteratively expanded "box", or is there a more elegant method available?
Thanks,
-Aubrey
Answer by jonas-echterhoff · Dec 06, 2009 at 05:57 PM
I don't think there is a more elegant method, but then, that script should be pretty short:
Bounds b = new Bounds(Vector3.zero, Vector3.zero);
foreach (Renderer r in FindObjectsOfType(typeof(Renderer))
b = b.Encapsulate(r.bounds);
If you want to do this every frame, you may consider caching the array of renderers, since FindObjectsOfType is slow, though.
Excellent! Far more elegant than the script I was envisioning :) Thanks again!
Yes, I only plan to run this script once
if you have objects that they have colliders without renderers, you should take care of them
You need to set the initial value of Bounds b based on the first Renderer. Otherwise, b.$$anonymous$$ values might stay zero if there are no negative coords. Similar for b.max if there aren't positive coords.
Answer by mnml_ · Oct 07, 2015 at 11:14 AM
var rnds = FindObjectsOfType<Renderer>();
if (rnds.Length == 0)
return; // nothing to see here, go on
var b = rnds[0].bounds;
for (int i = 1; i < rnds.Length; i++)
b.Encapsulate(rnds[i].bounds);
// now b holds the bounds of the scene
This is the correct answer, as it accounts for the $$anonymous$$ bounds properly.