// ╔════════════════════════════════════════════════════════════════╗ // ║ Copyright © 2025 NWH Coding d.o.o. All rights reserved. ║ // ║ Licensed under Unity Asset Store Terms of Service: ║ // ║ https://unity.com/legal/as-terms ║ // ║ Use permitted only in compliance with the License. ║ // ║ Distributed "AS IS", without warranty of any kind. ║ // ╚════════════════════════════════════════════════════════════════╝ #region using UnityEngine; #endregion namespace NWH.Common.Utility { /// /// Extension methods for GameObject and Transform operations. /// public static class GameObjectExtensions { /// /// Calculates the combined bounds of all MeshRenderers in a GameObject and its children. /// /// GameObject to calculate bounds for. /// Combined bounds encapsulating all child renderers. public static Bounds FindBoundsIncludeChildren(this GameObject gameObject) { Bounds bounds = new(); foreach (MeshRenderer mr in gameObject.GetComponentsInChildren()) { bounds.Encapsulate(mr.bounds); } return bounds; } /// /// Searches for a component in parent GameObjects, with option to include inactive objects. /// More flexible than Unity's built-in GetComponentInParent. /// /// Type of component to find. /// Starting transform. /// Include inactive GameObjects in search. /// First component of type T found in parents, or null if none found. public static T GetComponentInParent(this Transform transform, bool includeInactive = true) where T : Component { Transform 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 parents first, then children if not found. /// Combines functionality of GetComponentInParent and GetComponentInChildren. /// /// Type of component to find. /// Starting transform. /// Include inactive GameObjects in search. /// First component of type T found, or null if none found. 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; } } }