Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Added ability to pass in cached references to avoid costly lookup (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHodgson authored Jan 29, 2020
1 parent 4e74174 commit 2f9c1c2
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions XRTK-Core/Packages/com.xrtk.core/Extensions/TransformExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ private static IEnumerable<Transform> EnumerateHierarchyCore(this Transform root
/// <param name="syncTransform">
/// True, by default, this will sync the <see cref="transform"/> rotation to calculate the axis aligned orientation.
/// </param>
/// <param name="colliders">Optional cached collider collection.</param>
/// <returns>The total bounds of all colliders attached to this GameObject.
/// If no colliders attached, returns a bounds of center and extents 0</returns>
public static Bounds GetColliderBounds(this Transform transform, bool syncTransform = true)
public static Bounds GetColliderBounds(this Transform transform, bool syncTransform = true, Collider[] colliders = null)
{
// Store current rotation then zero out the rotation so that the bounds
// are computed when the object is in its 'axis aligned orientation'.
Expand All @@ -115,7 +116,10 @@ public static Bounds GetColliderBounds(this Transform transform, bool syncTransf
Physics.SyncTransforms(); // Update collider bounds
}

var colliders = transform.GetComponentsInChildren<Collider>();
if (colliders == null)
{
colliders = transform.GetComponentsInChildren<Collider>();
}

if (colliders.Length == 0) { return default; }

Expand Down Expand Up @@ -146,9 +150,10 @@ public static Bounds GetColliderBounds(this Transform transform, bool syncTransf
/// <param name="syncTransform">
/// True, by default, this will sync the <see cref="transform"/> rotation to calculate the axis aligned orientation.
/// </param>
/// <param name="renderers">Optional cached renderer collection.</param>
/// <returns>The total bounds of all renderers attached to this GameObject.
/// If no renderers attached, returns a bounds of center and extents 0</returns>
public static Bounds GetRenderBounds(this Transform transform, bool syncTransform = true)
public static Bounds GetRenderBounds(this Transform transform, bool syncTransform = true, Renderer[] renderers = null)
{
// Store current rotation then zero out the rotation so that the bounds
// are computed when the object is in its 'axis aligned orientation'.
Expand All @@ -160,7 +165,10 @@ public static Bounds GetRenderBounds(this Transform transform, bool syncTransfor
Physics.SyncTransforms(); // Update collider bounds
}

var renderers = transform.GetComponentsInChildren<Renderer>();
if (renderers == null)
{
renderers = transform.GetComponentsInChildren<Renderer>();
}

if (renderers.Length == 0) { return default; }

Expand Down Expand Up @@ -392,14 +400,21 @@ public static Transform FindCommonRoot(this Transform t1, Transform t2)
/// </summary>
/// <param name="transform"></param>
/// <param name="isActive"></param>
public static void SetCollidersActive(this Transform transform, bool isActive)
/// <param name="colliders">Optional cached collider collection to use instead of looking them all up.</param>
/// <returns>The collection of colliders that were acted on. This collection can be used later when calling this method again.</returns>
public static Collider[] SetCollidersActive(this Transform transform, bool isActive, Collider[] colliders = null)
{
var colliders = transform.GetComponentsInChildren<Collider>();
if (colliders == null)
{
colliders = transform.GetComponentsInChildren<Collider>();
}

for (int i = 0; i < colliders.Length; i++)
{
colliders[i].enabled = isActive;
}

return colliders;
}

/// <summary>
Expand Down

0 comments on commit 2f9c1c2

Please sign in to comment.