Find Bounds of a prefab of all the Meshes

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 …

2865425--209836--Screen Shot_00.jpg 2865425--209837--Screen Shot_01.jpg 2865425--209838--Screen Shot_02.jpg

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 …

OK, I worked it out … Here is the script if it can help anyone else.

#pragma strict

/*
    this script will find the bounds of a prefab, usfull for setting
    bounding boxes for light probes or anything else you need one for,
    the  GameObject this script is on must just be an empty GameObject
    and the rest of the parents, children, and grandchildren making
    up the prefab ..
*/

var _color : Color;

var meshRenererArray = List.<Renderer>();
var boundsArray = List.<Bounds>();
var totalBound : Bounds;

private var b : Bounds;
private var _center : Vector3 = Vector3.zero;

//              ----------------------------
function Start()
{
// accessing the parents, children, and grandchildren of objects
// use the next 2 lines else just to only access the children use
// "for ( var child: Transform in transform)"

    var allChildren = gameObject.GetComponentsInChildren(Transform);

    for ( var child : Transform in allChildren)
    {
        if(child.gameObject.GetComponent.<Renderer>() != null)
            meshRenererArray.Add(child.gameObject.GetComponent.<Renderer>());
    }
     for (var ii : int = 0; ii < meshRenererArray.Count; ii++)
     {
         boundsArray.Add(meshRenererArray[ii].bounds);
         _center += meshRenererArray[ii].bounds.center;
     }
//center is average center of children
     _center /= transform.childCount;
     totalBound = new Bounds(_center, Vector3.zero);

     for (var iii : int = 0; iii < boundsArray.Count; iii++)
     {
         b = new Bounds(boundsArray[iii].center, boundsArray[iii].size);
         totalBound.Encapsulate(boundsArray[iii]); 
    }
}
//              ----------------------------
function OnDrawGizmosSelected()
{
    Gizmos.color = _color;
    Gizmos.DrawCube (totalBound.center,totalBound.size);
}
//              ----------------------------

5 Likes