Example:
using UnityEngine;
using SK.Framework;
public class Foo : MonoBehaviour
{
private GameObject target;
private bool isDetected;
private void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//获取射线检测到的物体
target = ray.GetDetectedObject();
//获取射线检测到的物体 检测距离为10f
target = ray.GetDetectedObject(10f);
//获取射线检测到的物体 检测距离为10f 检测层级为Default层
target = ray.GetDetectedObject(10f, 1 << LayerMask.NameToLayer("Default"));
//判断射线是否检测到该物体
isDetected = ray.IsDetectedGameObject(gameObject);
//判断射线是否检测到该物体 检测距离为10f
isDetected = ray.IsDetectedGameObject(10f, gameObject);
//判断射线是否检测到该物体 检测距离为10f 检测层级为Default层
isDetected = ray.IsDetectedGameObejct(10f, 1 << LayerMask.NameToLayer("Default"), gameObject);
//判断射线检测到的物体是否包含指定组件
isDetected = ray.IsDetectedComponent(out Foo foo1);
//判断射线检测到的物体是否包含指定组件 检测距离为10f
isDetected = ray.IsDetectedComponent(10f, out Foo foo2);
//判断射线检测到的物体是否包含指定组件 检测距离为10f 检测层级为Default层
isDetected = ray.IsDetectedComponent(10f, 1 << LayerMask.NameToLayer("Default"), out Foo foo3);
}
}
Extension:
using UnityEngine;
namespace SK.Framework
{
/// <summary>
/// 射线相关拓展
/// </summary>
public static class RayExtension
{
/// <summary>
/// 获取检测到的物体
/// </summary>
/// <param name="self">射线</param>
/// <returns>检测到的物体</returns>
public static GameObject GetDetectedObject(this Ray self)
{
if(Physics.Raycast(self, out RaycastHit hitInfo))
{
return hitInfo.collider.gameObject;
}
return null;
}
/// <summary>
/// 获取检测到的物体
/// </summary>
/// <param name="self">射线</param>
/// <param name="maxDistance">检测的最大距离</param>
/// <returns>检测到的物体</returns>
public static GameObject GetDetectedObject(this Ray self, float maxDistance)
{
if (Physics.Raycast(self, out RaycastHit hitInfo, maxDistance))
{
return hitInfo.collider.gameObject;
}
return null;
}
/// <summary>
/// 获取检测到的物体
/// </summary>
/// <param name="self">射线</param>
/// <param name="maxDistance">检测的最大距离</param>
/// <param name="layerMask">检测的层级</param>
/// <returns>检测到的物体</returns>
public static GameObject GetDetectedObject(this Ray self, float maxDistance, int layerMask)
{
if (Physics.Raycast(self, out RaycastHit hitInfo, maxDistance, layerMask))
{
return hitInfo.collider.gameObject;
}
return null;
}
/// <summary>
/// 判断是否检测到目标物体
/// </summary>
/// <param name="self">射线</param>
/// <param name="target">目标物体</param>
/// <returns>若射线检测到目标物体返回true,否则返回false</returns>
public static bool IsDetectedGameObject(this Ray self, GameObject target)
{
return Physics.Raycast(self, out RaycastHit hitInfo) && hitInfo.collider.gameObject == target;
}
/// <summary>
/// 判断是否检测到目标物体
/// </summary>
/// <param name="self">射线</param>
/// <param name="maxDistance">检测的最大距离</param>
/// <param name="target">目标物体</param>
/// <returns>若射线检测到目标物体返回true,否则返回false</returns>
public static bool IsDetectedGameObject(this Ray self, float maxDistance, GameObject target)
{
return Physics.Raycast(self, out RaycastHit hitInfo, maxDistance) && hitInfo.collider.gameObject == target;
}
/// <summary>
/// 判断是否检测到目标物体
/// </summary>
/// <param name="self">射线</param>
/// <param name="maxDistance">检测的最大距离</param>
/// <param name="layerMask">检测的层级</param>
/// <param name="target">目标物体</param>
/// <returns>若射线检测到目标物体返回true,否则返回false</returns>
public static bool IsDetectedGameObejct(this Ray self, float maxDistance, int layerMask, GameObject target)
{
return Physics.Raycast(self, out RaycastHit hitInfo, maxDistance, layerMask) && hitInfo.collider.gameObject == target;
}
/// <summary>
/// 判断是否检测到目标组件
/// </summary>
/// <typeparam name="T">组件类型</typeparam>
/// <param name="self">射线</param>
/// <param name="t">组件</param>
/// <returns>若射线检测到目标组件返回true,否则返回false</returns>
public static bool IsDetectedComponent<T>(this Ray self, out T t) where T : Component
{
if (Physics.Raycast(self, out RaycastHit hitInfo))
{
t = hitInfo.collider.GetComponent<T>();
return t != null;
}
t = null;
return false;
}
/// <summary>
/// 判断是否检测到目标组件
/// </summary>
/// <typeparam name="T">组件类型</typeparam>
/// <param name="self">射线</param>
/// <param name="maxDistance">检测的最大距离</param>
/// <param name="t">组件</param>
/// <returns>若射线检测到目标组件返回true,否则返回false</returns>
public static bool IsDetectedComponent<T>(this Ray self, float maxDistance, out T t) where T : Component
{
if (Physics.Raycast(self, out RaycastHit hitInfo, maxDistance))
{
t = hitInfo.collider.GetComponent<T>();
return t != null;
}
t = null;
return false;
}
/// <summary>
/// 判断是否检测到目标组件
/// </summary>
/// <typeparam name="T">组件类型</typeparam>
/// <param name="self">射线</param>
/// <param name="maxDistance">检测的最大距离</param>
/// <param name="layerMask">检测的层级</param>
/// <param name="t">组件</param>
/// <returns>若射线检测到目标组件返回true,否则返回false</returns>
public static bool IsDetectedComponent<T>(this Ray self, float maxDistance, int layerMask, out T t) where T : Component
{
if (Physics.Raycast(self, out RaycastHit hitInfo, maxDistance, layerMask))
{
t = hitInfo.collider.GetComponent<T>();
return t != null;
}
t = null;
return false;
}
}
}
?欢迎关注公众号 “当代野生程序猿”
?
?
|