- Home /
Creating a bounding box for multiple objects using bounds
I'm working on scripting an Editor tool that automatically seeks out and adds an impromptu bounding box to groups of objects by making a default cube with renderer, encapsulating them, and turning off the cube's renderer. On the face, it seems like it should be fairly simple, but I've hit a number of problems.
I wrote this function to handle the encapsulation, it takes in two GameObjects, one being the parent object to the group of GameObjects that need bounding, and the other being a default cube that will be used for the bounding box (I created this before calling this function).
void Encap(GameObject obj, GameObject blocker) {
Object[] children = obj.GetComponentsInChildren<Renderer>();
foreach(Renderer child in children) {
if(child != null){
blocker.renderer.bounds.Encapsulate(child.bounds);
}
}
}
This should iterate through all the children and shape the blocker to encapsulate them all, instead it does absolutely nothing. After the script is complete, I have the same default cube I started with. I've tried a few different ways of doing this, (using colliders, using renderers, making a bounds and returning) but so far it always ends in a default cube with no changes. I put in some checks to see where it was going, and it's successfully finding each child object and running the encapsulate function, it's just not doing anything.
Any help would be appreciated, are bounds somehow broken in new versions of Unity, or am I just doing stupid?
maybe i m getting it wrong, but Encapsulate seems to accept point (Vector3) rather than bounds
so maybe it will work like that (did not test it, just guessiing) void Encap(GameObject obj, GameObject blocker) { Object[] children = obj.GetComponentsInChildren(); foreach(Renderer child in children) { if(child != null){ blocker.renderer.bounds.Encapsulate(child.bounds.$$anonymous$$); blocker.renderer.bounds.Encapsulate(child.bounds.max); } } }
Answer by joshmcewin · Mar 29, 2021 at 04:38 AM
Sorry For Bumping this, but i think this would be a Solution
private void Start()
{
BoxCollider boxCollider = gameObject.AddComponent<BoxCollider>();//to show it working
Bounds bounds = new Bounds();
bounds = Encap(transform, new Bounds());//recursion function, must get applyed to a Bounds
boxCollider.center = bounds.center;//apply the center and size
boxCollider.size = bounds.size;
}
Bounds Encap(Transform parent, Bounds blocker)
{
foreach (Transform child in parent)//gets all the children in the transform
{
Renderer renderer = child.GetComponent<Renderer>();
if (renderer != null)//checks if it has a renderer
{
blocker.Encapsulate(renderer.bounds);//makes blocker cover the child
}
blocker = Encap(child, blocker);//calls function on child to get all objects
}
return blocker;
}
Answer by Jake-L · Mar 01, 2011 at 03:23 PM
Is obj==blocker? Note that GetComponentsInChildren returns components from both child objects and itself. GetComponentInChildren behaves like expected and only returns childs.
The object group (obj) and the blocker (blocker) are two separate objects, so GetComponentsInChildren wouldn't pull up the blocker, it would pull up the parent of the object group however, which is what the "child != null" check is for, since the parent has no renderer.