using UnityEngine; namespace NWH.Common.Utility { public static class GameObjectExtensions { public static Bounds FindBoundsIncludeChildren(this GameObject gameObject) { Bounds bounds = new Bounds(); foreach (MeshRenderer mr in gameObject.GetComponentsInChildren()) { bounds.Encapsulate(mr.bounds); } return bounds; } /// /// Equal to GetComponentInParent but with option to include inactive in search. /// /// /// /// /// public static T GetComponentInParent(this Transform transform, bool includeInactive = true) where T : Component { var here = transform; T result = null; while (here && !result) { if (includeInactive || here.gameObject.activeSelf) { result = here.GetComponent(); } here = here.parent; } return result; } /// /// Searches for a component in children and parents (and self). /// /// /// /// /// public static T GetComponentInParentsOrChildren(this Transform transform, bool includeInactive = true) where T : Component { T result = transform.GetComponentInParent(includeInactive); if (result == null) { result = transform.GetComponentInChildren(includeInactive); } return result; } } }