74 lines
2.2 KiB
C#
74 lines
2.2 KiB
C#
|
using UnityEngine;
|
|||
|
public class RaycastDisplayer : MonoBehaviour
|
|||
|
{
|
|||
|
public static void DebugPhysicsRaycast(Ray ray, float distance, int layerMask)
|
|||
|
{
|
|||
|
#if UNITY_EDITOR
|
|||
|
distance = Mathf.Max(distance, 1000f);
|
|||
|
GetDisplayer().PhysicsRaycast(ray.origin, ray.direction.normalized * distance, layerMask);
|
|||
|
#else
|
|||
|
Module.Log.LogModule.ErrorLog("不允许在发布游戏中,使用射线Debug用的RaycastDisplayer!");
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
public static void DebugNavMeshRaycast(Ray ray, float distance, int areaMask)
|
|||
|
{
|
|||
|
#if UNITY_EDITOR
|
|||
|
distance = Mathf.Max(distance, 1000f);
|
|||
|
GetDisplayer().NavMeshRaycast(ray.origin, ray.direction.normalized * distance, areaMask);
|
|||
|
#else
|
|||
|
Module.Log.LogModule.ErrorLog("不允许在发布游戏中,使用射线Debug用的RaycastDisplayer!");
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
#if UNITY_EDITOR
|
|||
|
private static RaycastDisplayer _displayer;
|
|||
|
private Vector3 _start;
|
|||
|
private Vector3 _end;
|
|||
|
private Vector3? _hitPoint;
|
|||
|
|
|||
|
private static RaycastDisplayer GetDisplayer()
|
|||
|
{
|
|||
|
if (_displayer == null)
|
|||
|
{
|
|||
|
var displayObj = new GameObject("RaycastDisplayer");
|
|||
|
_displayer = displayObj.EnsureComponent<RaycastDisplayer>();
|
|||
|
}
|
|||
|
return _displayer;
|
|||
|
}
|
|||
|
|
|||
|
private void PhysicsRaycast(Vector3 start, Vector3 end, int layerMask)
|
|||
|
{
|
|||
|
_start = start;
|
|||
|
_end = end;
|
|||
|
RaycastHit hit;
|
|||
|
if (Physics.Raycast(start, end - start, out hit, Vector3.Distance(start, end), layerMask))
|
|||
|
_hitPoint = hit.point;
|
|||
|
else
|
|||
|
_hitPoint = null;
|
|||
|
}
|
|||
|
|
|||
|
private void NavMeshRaycast(Vector3 start, Vector3 end, int areaMask)
|
|||
|
{
|
|||
|
_start = start;
|
|||
|
_end = end;
|
|||
|
UnityEngine.AI.NavMeshHit hit;
|
|||
|
if (UnityEngine.AI.NavMesh.Raycast(start, end, out hit, areaMask))
|
|||
|
_hitPoint = hit.position;
|
|||
|
else
|
|||
|
_hitPoint = null;
|
|||
|
}
|
|||
|
|
|||
|
private void OnDrawGizmos()
|
|||
|
{
|
|||
|
Gizmos.color = Color.blue;
|
|||
|
Gizmos.DrawLine(_start, _end);
|
|||
|
if (_hitPoint != null)
|
|||
|
{
|
|||
|
Gizmos.color = Color.red;
|
|||
|
Gizmos.DrawSphere(_hitPoint.Value, 0.1f);
|
|||
|
}
|
|||
|
}
|
|||
|
#endif
|
|||
|
}
|