@@ -693,35 +693,29 @@ public static HashSet<GameObject> RemoveRedundantObjects(IEnumerable<UnityEngine
693
693
}
694
694
695
695
/// <summary>
696
- /// Recursively go through the hierarchy, adding up the bounding box centers
697
- /// of all the children, return number of transforms recursed .
696
+ /// Recursively go through the hierarchy, unioning the bounding box centers
697
+ /// of all the children, to find the combined bounds .
698
698
/// </summary>
699
- /// <returns>The number of GameObjects in hierarchy starting at t.</returns>
700
699
/// <param name="t">Transform.</param>
701
- /// <param name="centerSum">Sum of all the bounding box centers in hierarchy.</param>
702
- private int GetBoundingBoxCentersSum ( Transform t , ref Vector3 centerSum )
700
+ /// <param name="boundsUnion">The Bounds that is the Union of all the bounds on this transform's hierarchy.</param>
701
+ private void EncapsulateBounds ( Transform t , ref Bounds boundsUnion )
703
702
{
704
703
var bounds = GetBounds ( t ) ;
705
- if ( bounds . HasValue ) {
706
- centerSum += bounds . Value . center ;
707
- } else {
708
- centerSum += t . transform . position ;
709
- }
704
+ boundsUnion . Encapsulate ( bounds ) ;
710
705
711
- int count = 1 ;
712
706
foreach ( Transform child in t ) {
713
- count += GetBoundingBoxCentersSum ( child , ref centerSum ) ;
707
+ EncapsulateBounds ( child , ref boundsUnion ) ;
714
708
}
715
- return count ;
716
709
}
717
710
718
711
/// <summary>
719
712
/// Gets the bounds of a transform.
720
713
/// Looks first at the Renderer, then Mesh, then Collider.
714
+ /// Default to a bounds with center transform.position and size zero.
721
715
/// </summary>
722
- /// <returns>The bounds, or null if not found .</returns>
716
+ /// <returns>The bounds.</returns>
723
717
/// <param name="t">Transform.</param>
724
- private Bounds ? GetBounds ( Transform t )
718
+ private Bounds GetBounds ( Transform t )
725
719
{
726
720
var renderer = t . GetComponent < Renderer > ( ) ;
727
721
if ( renderer ) {
@@ -735,7 +729,7 @@ private int GetBoundingBoxCentersSum(Transform t, ref Vector3 centerSum)
735
729
if ( collider ) {
736
730
return collider . bounds ;
737
731
}
738
- return null ;
732
+ return new Bounds ( t . position , Vector3 . zero ) ;
739
733
}
740
734
741
735
/// <summary>
@@ -745,12 +739,18 @@ private int GetBoundingBoxCentersSum(Transform t, ref Vector3 centerSum)
745
739
/// <param name="gameObjects">Game objects.</param>
746
740
private Vector3 FindCenter ( IEnumerable < GameObject > gameObjects )
747
741
{
748
- Vector3 average = Vector3 . zero ;
749
- int count = 0 ;
742
+ Bounds bounds = new Bounds ( ) ;
743
+ // Assign the initial bounds to first GameObject's bounds
744
+ // (if we initialize the bounds to 0, then 0 will be part of the bounds)
745
+ foreach ( var go in gameObjects ) {
746
+ var tempBounds = GetBounds ( go . transform ) ;
747
+ bounds = new Bounds ( tempBounds . center , tempBounds . size ) ;
748
+ break ;
749
+ }
750
750
foreach ( var go in gameObjects ) {
751
- count += GetBoundingBoxCentersSum ( go . transform , ref average ) ;
751
+ EncapsulateBounds ( go . transform , ref bounds ) ;
752
752
}
753
- return average / count ;
753
+ return bounds . center ;
754
754
}
755
755
756
756
/// <summary>
0 commit comments