public static Bounds GetBounds(this GameObject go, GetBoundsMode mode = GetBoundsMode.CollidersAndRenderers) { Renderer[] renderers = go.GetComponentsInChildren <Renderer>(); Bounds bounds = new Bounds(); if (mode == GetBoundsMode.CollidersAndRenderers || mode == GetBoundsMode.OnlyRenderers) { for (int i = 0; i < renderers.Length; i++) { if (renderers[i].enabled && renderers[i].gameObject.activeInHierarchy) { if (bounds.extents == Vector3.zero) { bounds = renderers[i].bounds; } else { bounds.Encapsulate(renderers[i].bounds); } } } } if (mode == GetBoundsMode.CollidersAndRenderers || mode == GetBoundsMode.OnlyColliders) { Collider[] colliders = go.GetComponentsInChildren <Collider>(); for (int i = 0; i < colliders.Length; i++) { if (colliders[i].enabled && colliders[i].gameObject.activeInHierarchy) { if (bounds.extents == Vector3.zero) { bounds = colliders[i].bounds; } else { bounds.Encapsulate(colliders[i].bounds); } } } } return(bounds); }
/// <summary> /// Like BoundsProvider.GetBounds() but rotation, position or local scale won't affect the bounds. /// (The transform of the object is modified temporarily) /// </summary> /// <param name="obj"></param> /// <param name="mode"></param> /// <param name="ignoreTriggerColliders"></param> /// <param name="layerMask"></param> /// <returns></returns> public static Bounds GetLocalBounds(GameObject obj, GetBoundsMode mode = GetBoundsMode.Colliders, bool ignoreTriggerColliders = true, int layerMask = ~0) { var t = obj.transform; var oPos = t.position; var oRot = t.rotation; var oScale = t.localScale; t.position = Vector3.zero; t.rotation = Quaternion.identity; t.localScale = Vector3.one; var b = GetBounds(obj, mode, ignoreTriggerColliders, layerMask); t.position = oPos; t.rotation = oRot; t.localScale = oScale; return(b); }
/// <summary> /// Get the bounds of the GameObject using collider and renderer bounds in all children. Avoid using this method heavily. /// Renderers like ParticleSystems may cause undesired results. /// </summary> /// <param name="obj"></param> /// <param name="mode"></param> /// <param name="ignoreTriggerColliders"></param> /// <param name="layerMask"></param> /// <returns></returns> public static Bounds GetBounds(GameObject obj, GetBoundsMode mode = GetBoundsMode.Colliders, bool ignoreTriggerColliders = true, int layerMask = ~0) { Bounds bounds = new Bounds(); if ((mode & GetBoundsMode.Colliders) != 0) { var cols = obj.GetComponentsInChildren <Collider>(); foreach (var col in cols) { if (ignoreTriggerColliders && col.isTrigger) { continue; } if ((1 << col.gameObject.layer & layerMask) == 0) { continue; } if (bounds.extents == Vector3.zero) { bounds = col.bounds; } bounds.Encapsulate(col.bounds); } } if ((mode & GetBoundsMode.Renderers) != 0) { var rends = obj.GetComponentsInChildren <Renderer>(); foreach (var rend in rends) { if ((1 << rend.gameObject.layer & layerMask) == 0) { continue; } if (bounds.extents == Vector3.zero) { bounds = rend.bounds; } bounds.Encapsulate(rend.bounds); } } return(bounds); }