Skip to content

Commit

Permalink
Migrated missing extension from last bounds update and included new r…
Browse files Browse the repository at this point in the history
…everse hierarchy component search. (#62)
  • Loading branch information
SimonDarksideJ authored May 15, 2024
1 parent f0bbf4f commit 826a743
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
29 changes: 29 additions & 0 deletions Runtime/Extensions/BoundsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,35 @@ public static Vector3 CalculateFlattenVector(Vector3 size)
}
}

/// <summary>
/// Returns the bounds of the model and all its contained meshes.
/// </summary>
/// <param name="bounds">The input bounding volume.</param>
/// <param name="transform">The target transform to inspect and calculate the bounds from.</param>
/// <param name="includeInactive">Should the mesh query also include inactive components/objects.</param>
/// <returns>Returns a <see cref="Bounds"/> object with the updated bounds (used to apply to the Size and Center points of a bounding volume)</returns>
public static Bounds CalculateBoundsForModel(this Bounds bounds, Transform transform, bool includeInactive = false)
{
var renderers = transform.GetComponentsInChildren<Renderer>(includeInactive);

if (renderers.Length > 0)
{
bounds = renderers[0].bounds;

for (int i = 1; i < renderers.Length; i++)
{
bounds.Encapsulate(renderers[i].bounds);
}
}

foreach (Transform child in transform)
{
bounds.CalculateBoundsForModel(child);
}

return bounds;
}

#endregion

#region Private Static Functions
Expand Down
30 changes: 26 additions & 4 deletions Runtime/Extensions/GameObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public static bool TryGetComponentsInChildren<T>(this GameObject gameobject, out
}
}

if(componentsList.Count > 0)
if (componentsList.Count > 0)
{
components = componentsList.ToArray();
return true;
Expand All @@ -306,7 +306,7 @@ public static bool TryGetComponentsInChildren<T>(this GameObject gameobject, out
components = null;
return false;
}
}
}

/// <summary>
/// Gets a <see cref="Component"/> on the <see cref="GameObject"/>, its parent or any of its children if it is already attached to it.
Expand All @@ -323,6 +323,28 @@ public static bool TryGetComponentInChildrenAndParent<T>(this GameObject input,
return true;
}
return input.TryGetComponentInChildren<T>(out component);
}
}

/// <summary>
/// Gets a <see cref="Component"/> on the <see cref="GameObject"/>, or any parent in its hierarchy up to the root.
/// </summary>
/// <typeparam name="T">The type of the <see cref="Component"/> to lookup on the <see cref="GameObject"/>.</typeparam>
/// <param name="gameObject"><see cref="GameObject"/> instance.</param>
/// <param name="component">The <see cref="Component"/> instance found on <see cref="GameObject"/></param>
/// <returns>True if the Component was found on the GameObject or One of its parents</returns>
/// <remarks>Will only return the first instance if there are multiple in the GameObject Hierarchy</remarks>
public static bool TryGetComponentInParentHierarchy<T>(this GameObject input, out T component) where T : Component
{
if (input.TryGetComponent<T>(out component))
{
return true;
}
if (input.transform.parent.IsNull())
{
component = null;
return false;
}
return input.transform.parent.gameObject.TryGetComponentInParentHierarchy<T>(out component);
}
}
}
}

0 comments on commit 826a743

Please sign in to comment.