I have a bunch of prefabs as children of an empty game object, is there an easy way of determining what the total bounds of that grouped prefab is?
i wrote script that may help.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class PrefabAABB : MonoBehaviour
{
/// <summary>
/// local space Axis Aligned Bounding Box
/// </summary>
public Bounds bounds;
Transform _transform;
void OnDrawGizmos()
{
Gizmos.DrawWireCube(transform.position + bounds.center, bounds.size);
}
void Reset ()
{
RecalculateBounds();
}
public Bounds WorldBounds ()
{
if (_transform == null)
_transform = transform;
Bounds b = bounds;
b.center += _transform.position;
Vector3 size = b.size;
Vector3 tsize = _transform.lossyScale;
size.x *= tsize.x;
size.y *= tsize.y;
size.z *= tsize.z;
b.size = size;
return b;
}
[ContextMenu("Recalculate Bounds")]
public void RecalculateBounds ()
{
MeshFilter this_mf = GetComponent<MeshFilter>();
if (this_mf == null)
{
bounds = new Bounds(Vector3.zero, Vector3.zero);
}
else
{
bounds = this_mf.sharedMesh.bounds;
}
MeshFilter[] mfs = GetComponentsInChildren<MeshFilter>();
foreach (MeshFilter mf in mfs)
{
Vector3 pos = mf.transform.localPosition;
Bounds child_bounds = mf.sharedMesh.bounds;
child_bounds.center += pos;
bounds.Encapsulate(child_bounds);
}
}
#if UNITY_EDITOR
void Update()
{
if (Application.isPlaying) return;
RecalculateBounds();
}
#endif
}
You can construct an enclosing bounding box by instanciating Bounds and then repeatedly call Bounds.Encapsulate(bounds : Bounds) for all children to integrate their bounding boxes. See the links for details.
bounds as a square or as sphere?
search gameobjects in prefab, compare them all to find the one most at right, left, up down, fwds and backwards, that will give you a bounding box, or compare distance of all of them between each other, and keep the largest distance, use arrays.