I’m trying to find the size of an object that also includes its children. I’ve set up the following two functions, the first using Renderer.bounds and the second using MeshFilter.mesh.bounds:
//Find Combined Renderer Bounds
public Bounds FindCombinedRendererBounds (GameObject gameObject) {
Bounds combinedBounds;
Renderer startRenderer;
var renderers = gameObject.GetComponentsInChildren<Renderer>();
startRenderer = renderers[0];
combinedBounds = startRenderer.bounds;
foreach (Renderer renderer in renderers) {
if (renderer != startRenderer) {
combinedBounds.Encapsulate (renderer.bounds);
}
}
return combinedBounds;
}
//Find Combined Mesh Bounds
public Bounds FindCombinedMeshBounds (GameObject gameObject) {
Bounds combinedBounds;
MeshFilter startMeshFilter;
var meshFilters = gameObject.GetComponentsInChildren<MeshFilter>();
startMeshFilter = meshFilters[0];
combinedBounds = startMeshFilter.mesh.bounds;
foreach (MeshFilter meshFilter in meshFilters) {
if (meshFilter != startMeshFilter) {
combinedBounds.Encapsulate (meshFilter.mesh.bounds);
}
}
return combinedBounds;
}
I’ve then used the code to return the y size of the same object, but have found that both functions return different sizes. I’m confident that the Renderer version is returning the wrong size, but I’m not sure why. My assumption was that both the Renderer and Mesh versions should return the same value if given the same object. As a result I can’t use the Renderer function to return an accurate size for a group of objects.
Any thoughts?