/// <summary> /// Add a GameObject bounds to the static global bounds buffer, and then call this method /// for his children /// </summary> /// <param name="gameObject"></param> /// <param name="type"></param> private static void AddGameObjectToGlobalBounds(GameObject gameObject, EBoundsType type) { // we only look for the given type switch (type) { case EBoundsType.Collider: Collider2D collider = gameObject.GetComponent <Collider2D>(); if (collider != null) { AddBounds(collider.bounds); } break; case EBoundsType.Render: Renderer renderer = gameObject.GetComponent <Renderer>(); if (renderer != null) { AddBounds(renderer.bounds); } break; } // we recursivly call this method on every child for (int i = 0; i < gameObject.transform.childCount; i++) { AddGameObjectToGlobalBounds(gameObject.transform.GetChild(i).gameObject, type); } }
/// <summary> /// Search recursivly all the bounds from the given type. /// It merge it to get the global bounds of an object and all his descendants together. /// </summary> /// <param name="gameObject">The GO parent</param> /// <param name="type">The type to look for</param> /// <returns></returns> public static Bounds FindGlobalBounds(GameObject gameObject, EBoundsType type) { _globalBoundsIsFilled = false; // this flag is here to tell that the globalBounds buffer content is irelevant. AddGameObjectToGlobalBounds(gameObject, type); return(_globalBounds); // this buffer is filled in "AddGameObjectToGlobalBounds }