< Summary

Class:GDX.GameObjectExtensions
Assembly:GDX
File(s):D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/GameObjectExtensions.cs
Covered lines:0
Uncovered lines:24
Coverable lines:24
Total lines:117
Line coverage:0% (0 of 24)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:4
Method coverage:0% (0 of 4)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DestroyChildren(...)0%2100%
GetFirstComponentInChildrenComplex[T](...)0%30500%
GetOrAddComponent[T](...)0%6200%
GetScenePath(...)0%2100%

File(s)

D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/GameObjectExtensions.cs

#LineLine coverage
 1// Copyright (c) 2020-2022 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5#if !UNITY_DOTSRUNTIME
 6
 7using System.Runtime.CompilerServices;
 8using UnityEngine;
 9
 10namespace GDX
 11{
 12    /// <summary>
 13    ///     <see cref="UnityEngine.GameObject" /> Based Extension Methods
 14    /// </summary>
 15    /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception>
 16    [VisualScriptingCompatible(2)]
 17    public static class GameObjectExtensions
 18    {
 19        /// <summary>
 20        ///     Destroy child <see cref="GameObject" />.
 21        /// </summary>
 22        /// <param name="targetGameObject">The parent <see cref="GameObject" /> to look at.</param>
 23        /// <param name="deactivateBeforeDestroy">
 24        ///     Should the <paramref name="targetGameObject" />'s children be deactivated before
 25        ///     destroying? This can be used to immediately hide an object, that will be destroyed at the end of the fra
 26        /// </param>
 27        /// <param name="destroyInactive">Should inactive <see cref="GameObject" /> be destroyed as well?</param>
 28        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 29        public static void DestroyChildren(this GameObject targetGameObject, bool deactivateBeforeDestroy = true,
 30            bool destroyInactive = true)
 031        {
 032            targetGameObject.transform.DestroyChildren(deactivateBeforeDestroy, destroyInactive);
 033        }
 34
 35        /// <summary>
 36        ///     A slightly more complex version of <see cref="Component.GetComponentInChildren{T}(bool)" /> which allows
 37        ///     different hinted search options.
 38        /// </summary>
 39        /// <remarks>
 40        ///     <para>
 41        ///         If the <paramref name="maxLevelsOfRecursion" /> is set to 1; this will search the
 42        ///         <paramref name="targetGameObject" /> and its immediate children only.
 43        ///     </para>
 44        ///     <para>
 45        ///         The internal <see cref="Component.GetComponentInChildren{T}(bool)" /> has optimizations internally i
 46        ///         code which make it faster in different scenarios.
 47        ///     </para>
 48        /// </remarks>
 49        /// <param name="targetGameObject">The target <see cref="GameObject" /> to use as the base for the search.</para
 50        /// <param name="lookInChildrenFirst">
 51        ///     Should children <see cref="GameObject" /> be searched before the
 52        ///     <paramref name="targetGameObject" />'s <see cref="GameObject" />.
 53        /// </param>
 54        /// <param name="includeInactive">
 55        ///     Include inactive child <see cref="GameObject" />s when looking for the
 56        ///     <see cref="Component" />.
 57        /// </param>
 58        /// <param name="maxLevelsOfRecursion">
 59        ///     The maximum levels of recursion when looking for a <see cref="Component" />, -1 for
 60        ///     infinite recursion.
 61        /// </param>
 62        /// <typeparam name="T">The target <see cref="UnityEngine.Component" /> type that is being looked for.</typepara
 63        /// <returns>The first found <see cref="Component" />.</returns>
 64        public static T GetFirstComponentInChildrenComplex<T>(this GameObject targetGameObject,
 65            bool includeInactive = false, bool lookInChildrenFirst = false, int maxLevelsOfRecursion = -1)
 066        {
 67            // Make sure we have nothing to return if necessary.
 68            T returnComponent;
 69
 70            // Early out looking at the immediate target first.
 071            if (!lookInChildrenFirst)
 072            {
 073                returnComponent = targetGameObject.GetComponent<T>();
 074                if (returnComponent != null)
 075                {
 076                    return returnComponent;
 77                }
 078            }
 79
 080            returnComponent =
 81                targetGameObject.transform.GetFirstComponentInChildrenComplex<T>(includeInactive, 0,
 82                    maxLevelsOfRecursion);
 083            if (returnComponent != null)
 084            {
 085                return returnComponent;
 86            }
 87
 088            return !lookInChildrenFirst ? default : targetGameObject.GetComponent<T>();
 089        }
 90
 91        /// <summary>
 92        ///     Get a component by type from a <paramref name="targetGameObject" />, if it is not found add and return i
 93        /// </summary>
 94        /// <remarks>Adding a component at runtime is a performance nightmare. Use with caution!</remarks>
 95        /// <param name="targetGameObject">The <see cref="GameObject" /> that we should query for the component.</param>
 96        /// <typeparam name="T">The type of component.</typeparam>
 97        /// <returns>The component on the <paramref name="targetGameObject" />.</returns>
 98        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 99        public static T GetOrAddComponent<T>(this GameObject targetGameObject) where T : Component
 0100        {
 0101            T instance = targetGameObject.GetComponent<T>();
 0102            return instance ? instance : targetGameObject.AddComponent<T>();
 0103        }
 104
 105        /// <summary>
 106        ///     Get an in scene path to the <paramref name="targetGameObject" />.
 107        /// </summary>
 108        /// <param name="targetGameObject">The <see cref="GameObject" /> which to derive a path from.</param>
 109        /// <returns>A created path <see cref="System.String" />.</returns>
 110        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 111        public static string GetScenePath(this GameObject targetGameObject)
 0112        {
 0113            return targetGameObject.transform.GetScenePath();
 0114        }
 115    }
 116}
 117#endif