mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 22:55:42 +00:00
feat: add RaycastHit.GetComponent and RaycastHit.TryGetComponent
This commit is contained in:
parent
b2e4092ca7
commit
ac499537ed
@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
- X10D: Added `TextWriter.WriteLineNoAlloc(uint[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
- X10D: Added `TextWriter.WriteLineNoAlloc(uint[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
||||||
- X10D: Added `TextWriter.WriteLineNoAlloc(long[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
- X10D: Added `TextWriter.WriteLineNoAlloc(long[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
||||||
- X10D: Added `TextWriter.WriteLineNoAlloc(ulong[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
- X10D: Added `TextWriter.WriteLineNoAlloc(ulong[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
||||||
|
- X10D.Unity: Added `RaycastHit.GetComponent` and `RaycastHit.TryGetComponent`.
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
|
109
X10D.Unity/src/RaycastHitExtensions.cs
Normal file
109
X10D.Unity/src/RaycastHitExtensions.cs
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace X10D.Unity;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Extension methods for <see cref="RaycastHit" />.
|
||||||
|
/// </summary>
|
||||||
|
public static class RaycastHitExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the component of the specified type from the object that was hit by the raycast.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hit">The raycast hit.</param>
|
||||||
|
/// <typeparam name="T">The type of the component to retrieve.</typeparam>
|
||||||
|
/// <returns>
|
||||||
|
/// The component of the specified type from the object that was hit by the raycast, or <see langword="null" /> if no
|
||||||
|
/// component of the specified type was found.
|
||||||
|
/// </returns>
|
||||||
|
public static T? GetComponent<T>(this RaycastHit hit)
|
||||||
|
{
|
||||||
|
if (hit.transform == null)
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hit.transform.GetComponent<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the component of the specified type from the object that was hit by the raycast.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hit">The raycast hit.</param>
|
||||||
|
/// <param name="componentType">The type of the component to retrieve.</param>
|
||||||
|
/// <returns>
|
||||||
|
/// The component of the specified type from the object that was hit by the raycast, or <see langword="null" /> if no
|
||||||
|
/// component of the specified type was found.
|
||||||
|
/// </returns>
|
||||||
|
/// <exception cref="ArgumentNullException"><paramref name="componentType" /> is <see langword="null" />.</exception>
|
||||||
|
public static Component? GetComponent(this RaycastHit hit, Type componentType)
|
||||||
|
{
|
||||||
|
if (componentType is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(componentType));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hit.transform == null)
|
||||||
|
{
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hit.transform.GetComponent(componentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attempts to get the component of the specified type from the object that was hit by the raycast, and returns a value
|
||||||
|
/// that indicates whether the operation succeeded.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hit">The raycast hit.</param>
|
||||||
|
/// <param name="component">
|
||||||
|
/// When this method returns, contains the component of the specified type from the object that was hit by the raycast, or
|
||||||
|
/// <see langword="null" /> if no component of the specified type was found.
|
||||||
|
/// </param>
|
||||||
|
/// <typeparam name="T">The type of the component to retrieve.</typeparam>
|
||||||
|
/// <returns>
|
||||||
|
/// <see langword="true" /> if the component of the specified type was found; otherwise, <see langword="false" />.
|
||||||
|
/// </returns>
|
||||||
|
public static bool TryGetComponent<T>(this RaycastHit hit, [NotNullWhen(true)] out T? component)
|
||||||
|
{
|
||||||
|
if (hit.transform == null)
|
||||||
|
{
|
||||||
|
component = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hit.transform.TryGetComponent(out component);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attempts to get the component of the specified type from the object that was hit by the raycast, and returns a value
|
||||||
|
/// that indicates whether the operation succeeded.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hit">The raycast hit.</param>
|
||||||
|
/// <param name="componentType">The type of the component to retrieve.</param>
|
||||||
|
/// <param name="component">
|
||||||
|
/// When this method returns, contains the component of the specified type from the object that was hit by the raycast, or
|
||||||
|
/// <see langword="null" /> if no component of the specified type was found.
|
||||||
|
/// </param>
|
||||||
|
/// <returns>
|
||||||
|
/// <see langword="true" /> if the component of the specified type was found; otherwise, <see langword="false" />.
|
||||||
|
/// </returns>
|
||||||
|
/// <exception cref="ArgumentNullException"><paramref name="componentType" /> is <see langword="null" />.</exception>
|
||||||
|
public static bool TryGetComponent(this RaycastHit hit, Type componentType, [NotNullWhen(true)] out Component? component)
|
||||||
|
{
|
||||||
|
if (componentType is null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(componentType));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hit.transform == null)
|
||||||
|
{
|
||||||
|
component = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return hit.transform.TryGetComponent(componentType, out component);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user