Skip to content

Commit

Permalink
GetComponents no longer returns components on disabled gameobjects un…
Browse files Browse the repository at this point in the history
…less includeInactive is true
  • Loading branch information
michaelsakharov committed Apr 14, 2024
1 parent 13de755 commit 7b4b2a7
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions Prowl.Runtime/GameObject/GameObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,14 +408,14 @@ public IEnumerable<T> GetComponents<T>() where T : MonoBehaviour
}
}

public T? GetComponentInParent<T>(bool includeSelf = true) where T : MonoBehaviour => (T)GetComponentInParent(typeof(T), includeSelf);
public T? GetComponentInParent<T>(bool includeSelf = true, bool includeInactive = false) where T : MonoBehaviour => (T)GetComponentInParent(typeof(T), includeSelf, includeInactive);

public MonoBehaviour? GetComponentInParent(Type componentType, bool includeSelf = true)
public MonoBehaviour? GetComponentInParent(Type componentType, bool includeSelf = true, bool includeInactive = false)
{
if (componentType == null) return null;
// First check the current Object
MonoBehaviour component;
if (includeSelf) {
if (includeSelf && enabledInHierarchy) {
component = GetComponent(componentType);
if (component != null)
return component;
Expand All @@ -424,60 +424,69 @@ public IEnumerable<T> GetComponents<T>() where T : MonoBehaviour
GameObject parent = this;
while ((parent = parent.parent) != null)
{
component = parent.GetComponent(componentType);
if (component != null)
return component;
if (parent.enabledInHierarchy || includeInactive)
{
component = parent.GetComponent(componentType);
if (component != null)
return component;
}
}
return null;
}

public IEnumerable<T> GetComponentsInParent<T>(bool includeSelf = true) where T : MonoBehaviour
public IEnumerable<T> GetComponentsInParent<T>(bool includeSelf = true, bool includeInactive = false) where T : MonoBehaviour
{
// First check the current Object
if (includeSelf)
if (includeSelf && enabledInHierarchy)
foreach (var component in GetComponents<T>())
yield return component;
// Now check all parents
GameObject parent = this;
while ((parent = parent.parent) != null) {
foreach (var component in parent.GetComponents<T>())
yield return component;
if(parent.enabledInHierarchy || includeInactive)
foreach (var component in parent.GetComponents<T>())
yield return component;
}
}

public T? GetComponentInChildren<T>(bool includeSelf = true) where T : MonoBehaviour => (T)GetComponentInChildren(typeof(T), includeSelf);
public T? GetComponentInChildren<T>(bool includeSelf = true, bool includeInactive = false) where T : MonoBehaviour => (T)GetComponentInChildren(typeof(T), includeSelf, includeInactive);

public MonoBehaviour GetComponentInChildren(Type componentType, bool includeSelf = true)
public MonoBehaviour GetComponentInChildren(Type componentType, bool includeSelf = true, bool includeInactive = false)
{
if (componentType == null) return null;
// First check the current Object
MonoBehaviour component;
if (includeSelf) {
if (includeSelf && enabledInHierarchy) {
component = GetComponent(componentType);
if (component != null)
return component;
}
// Now check all children
foreach (var child in children)
{
component = child.GetComponent(componentType) ?? child.GetComponentInChildren(componentType);
if (component != null)
return component;
if (parent.enabledInHierarchy || includeInactive)
{
component = child.GetComponent(componentType) ?? child.GetComponentInChildren(componentType);
if (component != null)
return component;
}
}
return null;
}


public IEnumerable<T> GetComponentsInChildren<T>(bool includeSelf = true) where T : MonoBehaviour
public IEnumerable<T> GetComponentsInChildren<T>(bool includeSelf = true, bool includeInactive = false) where T : MonoBehaviour
{
// First check the current Object
if (includeSelf)
if (includeSelf && enabledInHierarchy)
foreach (var component in GetComponents<T>())
yield return component;
// Now check all children
foreach (var child in children) {
foreach (var component in child.GetComponentsInChildren<T>())
yield return component;
foreach (var child in children)
{
if (parent.enabledInHierarchy || includeInactive)
foreach (var component in child.GetComponentsInChildren<T>())
yield return component;
}
}

Expand Down

0 comments on commit 7b4b2a7

Please sign in to comment.