Hi,
I have a lot of rooms I made and saved as prefabs, what I’d like to do is get the bounds of the rooms.
I’ve tried this script
#pragma strict
var _color : Color;
var meshfilterArray : MeshFilter[];
var boundsArray : Bounds[];
var totalBound : Bounds;
var bound : Bounds;
var mf : MeshFilter;
var scale : float;
var b : Bounds;
function Start ()
{
meshfilterArray = this.gameObject.GetComponentsInChildren.<MeshFilter>();
findMeshBounds();
}
function findMeshBounds ()
{
boundsArray = new Bounds[meshfilterArray.Length];
for (var i=0; i < meshfilterArray.Length; i++)
{
boundsArray[i] = meshfilterArray[i].mesh.bounds;
}
totalBound = new Bounds (this.transform.position, Vector3.zero);
for (var ii=1; ii < boundsArray.Length; ii++)
{
bound = boundsArray[ii];
mf = meshfilterArray[ii];
scale = mf.transform.localScale.x;
b = new Bounds(bound.center * scale, bound.size * scale);
b = new Bounds(bound.center, bound.size);
totalBound.Encapsulate(b);
}
Debug.Log("The local bounds of this model is " + totalBound);
}
function OnDrawGizmos ()
{
Gizmos.color = _color;
Gizmos.DrawCube (transform.position, totalBound.extents);
}
But the results are below …

But it explodes the pieces of the prefab and the bounds are strange, can anyone point me in the right direction please, thanks.
PS If I use renderer
var combinedBounds = renderer.bounds;
var renderers = GetComponentsInChildren(Renderer);
for (var render : Renderer in renderers) {
if (render != renderer) combinedBounds.Encapsulate(render.bounds);
}
I get a small box not covering the complete prefab …
