- Home /
Getting bounds of a prefab with multiple children
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?
Answer by lassade · Feb 08, 2014 at 01:49 AM
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
}
Answer by flberger · Jun 12, 2013 at 01:30 PM
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.
Here's the code to do what flberger said:
public Bounds bounds;
public void CalculateBounds ()
{
bounds.size = Vector3.zero; // reset
Collider2D[] colliders = GetComponentsInChildren<Collider2D>();
foreach (Collider2D col in colliders)
{
bounds.Encapsulate(col.bounds);
}
}
Thanks mate, you just saved us from hours of problem solving once again. You're a legend!
Answer by MountDoomTeam · Oct 15, 2012 at 09:21 PM
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.
I wanted to do it with square bounds, so your saying I can just compare the bounding boxed of all children to find the greatest extends? Is the bounds property aabb?
i would put all the children objects into an array, and then find the one with the lowest and highest z, for example sort array vector3.z by value from low to high and choose number 1 and the last one.
do the same for x and y and you will have a bounding box parralel with x y and z,
then you can take a gameobject, put it in the middle of x y and z that you found, and resize it to the difference between -z and z and -x and x etc.
Your answer
Follow this Question
Related Questions
Size of a prefab instance is always (0,0,0) 1 Answer
Deleting a Prefab when it is dragged to a GUI Texture 0 Answers
Collider.bounds position is wrong? 3 Answers
How can I determine an object of prefab's size (for positioning when instantiating them) 2 Answers
Array of prefabs with different sizes stick to each other 0 Answers