@@ -694,37 +694,50 @@ public static HashSet<GameObject> RemoveRedundantObjects(IEnumerable<UnityEngine
694
694
695
695
/// <summary>
696
696
/// Recursively go through the hierarchy, adding up the bounding box centers
697
- /// of all the children, to help find the center of all selected objects .
697
+ /// of all the children, return number of transforms recursed .
698
698
/// </summary>
699
699
/// <returns>The number of GameObjects in hierarchy starting at t.</returns>
700
700
/// <param name="t">Transform.</param>
701
- /// <param name="centerAverage ">Sum of all the bounding box centers in hierarchy.</param>
702
- private int FindCenterRecursive ( Transform t , ref Vector3 centerAverage )
701
+ /// <param name="centerSum ">Sum of all the bounding box centers in hierarchy.</param>
702
+ private int GetBoundingBoxCentersSum ( Transform t , ref Vector3 centerSum )
703
703
{
704
- var renderer = t . GetComponent < Renderer > ( ) ;
705
- if ( renderer ) {
706
- centerAverage += renderer . bounds . center ;
704
+ var bounds = GetBounds ( t ) ;
705
+ if ( bounds . HasValue ) {
706
+ centerSum += bounds . Value . center ;
707
707
} else {
708
- var mesh = t . GetComponent < Mesh > ( ) ;
709
- if ( mesh ) {
710
- centerAverage += mesh . bounds . center ;
711
- } else {
712
- var collider = t . GetComponent < Collider > ( ) ;
713
- if ( collider ) {
714
- centerAverage += collider . bounds . center ;
715
- } else {
716
- centerAverage += t . transform . position ;
717
- }
718
- }
708
+ centerSum += t . transform . position ;
719
709
}
720
710
721
711
int count = 1 ;
722
712
foreach ( Transform child in t ) {
723
- count += FindCenterRecursive ( child , ref centerAverage ) ;
713
+ count += GetBoundingBoxCentersSum ( child , ref centerSum ) ;
724
714
}
725
715
return count ;
726
716
}
727
717
718
+ /// <summary>
719
+ /// Gets the bounds of a transform.
720
+ /// Looks first at the Renderer, then Mesh, then Collider.
721
+ /// </summary>
722
+ /// <returns>The bounds, or null if not found.</returns>
723
+ /// <param name="t">Transform.</param>
724
+ private Bounds ? GetBounds ( Transform t )
725
+ {
726
+ var renderer = t . GetComponent < Renderer > ( ) ;
727
+ if ( renderer ) {
728
+ return renderer . bounds ;
729
+ }
730
+ var mesh = t . GetComponent < Mesh > ( ) ;
731
+ if ( mesh ) {
732
+ return mesh . bounds ;
733
+ }
734
+ var collider = t . GetComponent < Collider > ( ) ;
735
+ if ( collider ) {
736
+ return collider . bounds ;
737
+ }
738
+ return null ;
739
+ }
740
+
728
741
/// <summary>
729
742
/// Finds the center of a group of GameObjects.
730
743
/// </summary>
@@ -735,7 +748,7 @@ private Vector3 FindCenter(IEnumerable<GameObject> gameObjects)
735
748
Vector3 average = Vector3 . zero ;
736
749
int count = 0 ;
737
750
foreach ( var go in gameObjects ) {
738
- count += FindCenterRecursive ( go . transform , ref average ) ;
751
+ count += GetBoundingBoxCentersSum ( go . transform , ref average ) ;
739
752
}
740
753
return average / count ;
741
754
}
0 commit comments