1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 19:18:46 +00:00

Add GetComponentsInChildrenOnly<T>

This commit is contained in:
Oliver Booth 2022-05-08 00:06:39 +01:00
parent 154b742c75
commit 60a3459663
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
3 changed files with 53 additions and 1 deletions

View File

@ -3,6 +3,8 @@
## [3.1.0-nightly]
### Added
- Readded Unity support:
- Added `Component.GetComponentsInChildrenOnly<T>()`
- Added `GameObject.GetComponentsInChildrenOnly<T>()`
- Added `GameObject.LookAt(GameObject[, Vector3])`
- Added `GameObject.LookAt(Transform[, Vector3])`
- Added `GameObject.LookAt(Vector3[, Vector3])`

View File

@ -0,0 +1,21 @@
using UnityEngine;
namespace X10D.Unity;
/// <summary>
/// Extension methods for <see cref="Component" />.
/// </summary>
public static class ComponentExtensions
{
/// <summary>
/// Returns an array of components of the specified type, excluding components that live on the object to which this
/// component is attached.
/// </summary>
/// <param name="component">The component whose child components to retrieve.</param>
/// <typeparam name="T">The type of the components to retrieve.</typeparam>
/// <returns>An array <typeparamref name="T" /> representing the child components.</returns>
public static T[] GetComponentsInChildrenOnly<T>(this Component component)
{
return component.gameObject.GetComponentsInChildrenOnly<T>();
}
}

View File

@ -1,4 +1,4 @@
using UnityEngine;
using UnityEngine;
namespace X10D.Unity;
@ -7,6 +7,35 @@ namespace X10D.Unity;
/// </summary>
public static class GameObjectExtensions
{
/// <summary>
/// Returns an array of components of the specified type, excluding components that live on this game object.
/// </summary>
/// <param name="gameObject">The game object whose child components to retrieve.</param>
/// <typeparam name="T">The type of the components to retrieve.</typeparam>
/// <returns>An array <typeparamref name="T" /> representing the child components.</returns>
public static T[] GetComponentsInChildrenOnly<T>(this GameObject gameObject)
{
var components = new List<T>(gameObject.GetComponentsInChildren<T>());
for (var index = 0; index < components.Count; index++)
{
if (components[index] is not Component childComponent)
{
// this shouldn't happen, since you can't add a non-Component to a game object,
// but GetComponentsInChildren<T> is not constrained, so this method shouldn't be either
continue;
}
if (childComponent.transform.parent != gameObject.transform)
{
components.RemoveAt(index);
index--;
}
}
return components.ToArray();
}
/// <summary>
/// Rotates the transform component of this game object so the forward vector points at another game object.
/// </summary>