| | 1 | | // Copyright (c) 2020-2024 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 | |
|
| | 7 | | using System.Runtime.CompilerServices; |
| | 8 | | using System.Text; |
| | 9 | | using UnityEditor; |
| | 10 | | using UnityEngine; |
| | 11 | |
|
| | 12 | | namespace GDX |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// <see cref="UnityEngine.Transform" /> Based Extension Methods |
| | 16 | | /// </summary> |
| | 17 | | /// <exception cref="UnsupportedRuntimeException">Not supported on DOTS Runtime.</exception> |
| | 18 | | [VisualScriptingCompatible(2)] |
| | 19 | | public static class TransformExtensions |
| | 20 | | { |
| | 21 | | /// <summary> |
| | 22 | | /// Destroy child <see cref="Transform" />. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="targetTransform">The parent <see cref="Transform" /> to look at.</param> |
| | 25 | | /// <param name="deactivateBeforeDestroy"> |
| | 26 | | /// Should the <paramref name="targetTransform" /> children's |
| | 27 | | /// <see cref="GameObject" />s be deactivated before destroying? This can be used to immediately hide an obj |
| | 28 | | /// will be destroyed at the end of the frame. |
| | 29 | | /// </param> |
| | 30 | | /// <param name="destroyInactive">Should inactive <see cref="GameObject" /> be destroyed as well?</param> |
| | 31 | | /// <param name="immediateMode">Should the destroy be done immediately? This is useful for author time calls.</p |
| | 32 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 33 | | public static void DestroyChildren(this Transform targetTransform, bool deactivateBeforeDestroy = true, |
| | 34 | | bool destroyInactive = true, bool immediateMode = false) |
| 3 | 35 | | { |
| 3 | 36 | | int count = targetTransform.childCount; |
| 24 | 37 | | for (int i = count - 1; i >= 0; i--) |
| 9 | 38 | | { |
| 9 | 39 | | GameObject childObject = targetTransform.GetChild(i).gameObject; |
| 9 | 40 | | if (!destroyInactive && !childObject.activeInHierarchy) |
| 2 | 41 | | { |
| 2 | 42 | | continue; |
| | 43 | | } |
| | 44 | |
|
| 7 | 45 | | if (deactivateBeforeDestroy) |
| 3 | 46 | | { |
| 3 | 47 | | childObject.SetActive(false); |
| 3 | 48 | | } |
| | 49 | |
|
| 7 | 50 | | if (immediateMode) |
| 5 | 51 | | { |
| 5 | 52 | | Object.DestroyImmediate(childObject); |
| 5 | 53 | | } |
| | 54 | | else |
| 2 | 55 | | { |
| 2 | 56 | | Object.Destroy(childObject); |
| 2 | 57 | | } |
| 7 | 58 | | } |
| 3 | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Get the number of immediate children active. |
| | 63 | | /// </summary> |
| | 64 | | /// <param name="targetTransform">The transform to look at's children.</param> |
| | 65 | | /// <returns>The number of active children transforms.</returns> |
| | 66 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 67 | | public static int GetActiveChildCount(this Transform targetTransform) |
| 1 | 68 | | { |
| 1 | 69 | | int counter = 0; |
| 1 | 70 | | int childCount = targetTransform.childCount; |
| 8 | 71 | | for (int i = 0; i < childCount; i++) |
| 3 | 72 | | { |
| 3 | 73 | | if (targetTransform.GetChild(i).gameObject.activeSelf) |
| 2 | 74 | | { |
| 2 | 75 | | counter++; |
| 2 | 76 | | } |
| 3 | 77 | | } |
| | 78 | |
|
| 1 | 79 | | return counter; |
| 1 | 80 | | } |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Search recursively for a <see cref="Component" /> on the <paramref name="targetTransform" />. |
| | 84 | | /// </summary> |
| | 85 | | /// <param name="targetTransform">The target <see cref="Transform" /> to use as the base for the search.</param> |
| | 86 | | /// <param name="includeInactive"> |
| | 87 | | /// Include inactive child <see cref="GameObject" />s when looking for the |
| | 88 | | /// <see cref="Component" />. |
| | 89 | | /// </param> |
| | 90 | | /// <param name="currentDepth">Current level of recursion.</param> |
| | 91 | | /// <param name="maxLevelsOfRecursion"> |
| | 92 | | /// The maximum levels of recursion when looking for a <see cref="Component" />, -1 for |
| | 93 | | /// infinite recursion. |
| | 94 | | /// </param> |
| | 95 | | /// <typeparam name="T">The target <see cref="UnityEngine.Component" /> type that is being looked for.</typepara |
| | 96 | | /// <returns>The first found <see cref="Component" />.</returns> |
| | 97 | | public static T GetFirstComponentInChildrenComplex<T>(this Transform targetTransform, bool includeInactive, |
| | 98 | | int currentDepth, int maxLevelsOfRecursion = -1) where T : Component |
| 6 | 99 | | { |
| | 100 | | // Increase depth count |
| 6 | 101 | | currentDepth++; |
| | 102 | |
|
| 6 | 103 | | if (maxLevelsOfRecursion >= 0 && currentDepth > maxLevelsOfRecursion) |
| 0 | 104 | | { |
| 0 | 105 | | return default; |
| | 106 | | } |
| | 107 | |
|
| 6 | 108 | | int cachedChildCount = targetTransform.childCount; |
| 20 | 109 | | for (int i = 0; i < cachedChildCount; i++) |
| 8 | 110 | | { |
| 8 | 111 | | Transform transformToCheck = targetTransform.GetChild(i); |
| | 112 | |
|
| | 113 | | // Don't include disabled transforms |
| 8 | 114 | | if (!transformToCheck.gameObject.activeSelf && |
| | 115 | | !includeInactive) |
| 0 | 116 | | { |
| 0 | 117 | | continue; |
| | 118 | | } |
| | 119 | |
|
| | 120 | | // Lets check the current transform for the component. |
| 8 | 121 | | T returnComponent = transformToCheck.GetComponent<T>(); |
| | 122 | |
|
| | 123 | | // Its important to use the Equals here, not a != |
| 8 | 124 | | if (returnComponent != null) |
| 3 | 125 | | { |
| 3 | 126 | | return returnComponent; |
| | 127 | | } |
| | 128 | |
|
| | 129 | | // OK, time to deep dive. |
| 5 | 130 | | if (maxLevelsOfRecursion >= 0 && currentDepth >= maxLevelsOfRecursion) |
| 2 | 131 | | { |
| 2 | 132 | | continue; |
| | 133 | | } |
| | 134 | |
|
| 3 | 135 | | returnComponent = GetFirstComponentInChildrenComplex<T>(transformToCheck, includeInactive, currentDepth, |
| | 136 | | maxLevelsOfRecursion); |
| 3 | 137 | | if (returnComponent != null) |
| 1 | 138 | | { |
| 1 | 139 | | return returnComponent; |
| | 140 | | } |
| 2 | 141 | | } |
| | 142 | |
|
| 2 | 143 | | return default; |
| 6 | 144 | | } |
| | 145 | |
|
| | 146 | | /// <summary> |
| | 147 | | /// Get an in scene path to the <paramref name="targetTransform" />. |
| | 148 | | /// </summary> |
| | 149 | | /// <param name="targetTransform">The <see cref="Transform" /> which to derive a path from.</param> |
| | 150 | | /// <returns>A created path <see cref="System.String" />.</returns> |
| | 151 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 152 | | public static string GetScenePath(this Transform targetTransform) |
| 1 | 153 | | { |
| 1 | 154 | | StringBuilder stringBuilder = new StringBuilder(); |
| | 155 | | #if UNITY_EDITOR |
| 1 | 156 | | Transform originalTransform = targetTransform; |
| | 157 | | #endif // UNITY_EDITOR |
| 2 | 158 | | while (targetTransform != null) |
| 1 | 159 | | { |
| 1 | 160 | | stringBuilder.Insert(0, targetTransform.name); |
| 1 | 161 | | stringBuilder.Insert(0, '/'); |
| 1 | 162 | | targetTransform = targetTransform.parent; |
| 1 | 163 | | } |
| | 164 | | #if UNITY_EDITOR |
| 1 | 165 | | if (originalTransform && |
| | 166 | | EditorUtility.IsPersistent(originalTransform)) |
| 0 | 167 | | { |
| 0 | 168 | | stringBuilder.Append(" [P]"); |
| 0 | 169 | | } |
| | 170 | | #endif // UNITY_EDITOR |
| 1 | 171 | | return stringBuilder.ToString(); |
| 1 | 172 | | } |
| | 173 | | } |
| | 174 | | } |
| | 175 | | // ReSharper disable once CommentTypo |
| | 176 | | #endif // !UNITY_DOTSRUNTIME |