From 6e389a0ec93f2fef644ad9d1b026b5a96ceb00a4 Mon Sep 17 00:00:00 2001 From: "Simon (Darkside) Jackson" Date: Fri, 31 May 2024 20:27:13 +0100 Subject: [PATCH] Renamed `TryGetComponentInParentHierarchy` to `TryFindAncestorComponent` and properly implemented `FindAncestorComponent` --- Runtime/Extensions/GameObjectExtensions.cs | 66 +++++++++++++--------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/Runtime/Extensions/GameObjectExtensions.cs b/Runtime/Extensions/GameObjectExtensions.cs index 3adc504..d61be16 100644 --- a/Runtime/Extensions/GameObjectExtensions.cs +++ b/Runtime/Extensions/GameObjectExtensions.cs @@ -112,12 +112,44 @@ public static void ApplyToHierarchy(this GameObject root, Action act /// Find the first component of type in the ancestors of the specified game object. /// /// Type of component to find. - /// Game object for which ancestors must be considered. + /// Game object for which ancestors must be considered. /// Indicates whether the specified game object should be included. /// The component of type . Null if it none was found. - public static T FindAncestorComponent(this GameObject gameObject, bool includeSelf = true) where T : Component + public static T FindAncestorComponent(this GameObject input, bool includeSelf = true) { - return gameObject.transform.FindAncestorComponent(includeSelf); + if (includeSelf && input.TryGetComponent(out var component)) + { + return component; + } + if (input.transform.parent.IsNull()) + { + return default(T); + } + + return input.transform.parent.gameObject.FindAncestorComponent(true); + } + + /// + /// Gets a on the , or any parent in its hierarchy up to the root. + /// + /// The type of the to lookup on the . + /// instance. + /// The instance found on + /// Indicates whether the specified game object should be included. + /// True if the Component was found on the GameObject or One of its parents + /// Will only return the first instance if there are multiple in the GameObject Hierarchy + public static bool TryFindAncestorComponent(this GameObject input, out T component, bool includeSelf = true) + { + if (includeSelf && input.TryGetComponent(out component)) + { + return true; + } + if (input.transform.parent.IsNull()) + { + component = default(T); + return false; + } + return input.transform.parent.gameObject.TryFindAncestorComponent(out component, true); } /// @@ -254,7 +286,7 @@ public static void Validate(this GameObject gameobject, [CallerFilePath] string /// The instance found on /// True if the Component was found on the GameObject or One of its children /// Will only return the first instance if there are multiple in the GameObject Hierarchy - public static bool TryGetComponentInChildren(this GameObject gameObject, out T component) where T : Component + public static bool TryGetComponentInChildren(this GameObject gameObject, out T component) { if (gameObject.TryGetComponent(out component)) { @@ -280,7 +312,7 @@ public static bool TryGetComponentInChildren(this GameObject gameObject, out /// The instance found on /// True if the Component was found on the GameObject or One of its children /// Will only return the first instance if there are multiple in the GameObject Hierarchy - public static bool TryGetComponentsInChildren(this GameObject gameobject, out T[] components) where T : Component + public static bool TryGetComponentsInChildren(this GameObject gameobject, out T[] components) { List componentsList = new List(); if (gameobject.TryGetComponent(out var component)) @@ -316,7 +348,7 @@ public static bool TryGetComponentsInChildren(this GameObject gameobject, out /// The instance found on /// True if the Component was found on the GameObject or One of its children /// Will only return the first instance if there are multiple in the GameObject Hierarchy - public static bool TryGetComponentInChildrenAndParent(this GameObject input, out T component) where T : Component + public static bool TryGetComponentInChildrenAndParent(this GameObject input, out T component) { if (input.transform.parent.IsNotNull() && input.transform.parent.gameObject.TryGetComponent(out component)) { @@ -324,27 +356,5 @@ public static bool TryGetComponentInChildrenAndParent(this GameObject input, } return input.TryGetComponentInChildren(out component); } - - /// - /// Gets a on the , or any parent in its hierarchy up to the root. - /// - /// The type of the to lookup on the . - /// instance. - /// The instance found on - /// True if the Component was found on the GameObject or One of its parents - /// Will only return the first instance if there are multiple in the GameObject Hierarchy - public static bool TryGetComponentInParentHierarchy(this GameObject input, out T component) where T : Component - { - if (input.TryGetComponent(out component)) - { - return true; - } - if (input.transform.parent.IsNull()) - { - component = null; - return false; - } - return input.transform.parent.gameObject.TryGetComponentInParentHierarchy(out component); - } } } \ No newline at end of file