Combined Bounds Center
Combine world-space renderer bounds to find the center and full size of the smallest axis-aligned box that contains them.
View the Unity C# source on GitHubResult
The same calculation as a Unity C# helper.
using UnityEngine;
public static class CombinedBounds
{
public static Bounds Combine(Vector3 objectPosition, params Bounds[] rendererBounds)
{
if (rendererBounds.Length == 0)
return new Bounds(objectPosition, Vector3.zero);
Bounds result = rendererBounds[0];
for (int i = 1; i < rendererBounds.Length; i++)
result.Encapsulate(rendererBounds[i]);
return result;
}
public static Bounds FromRenderers(Transform root)
{
Renderer[] renderers = root.GetComponentsInChildren<Renderer>();
var bounds = new Bounds[renderers.Length];
for (int i = 0; i < renderers.Length; i++)
bounds[i] = renderers[i].bounds;
return Combine(root.position, bounds);
}
}