mirror of
https://github.com/DCFApixels/Unity-DebugX.git
synced 2025-09-17 09:24:36 +08:00
66 lines
2.4 KiB
C#
66 lines
2.4 KiB
C#
using DCFApixels.DebugXCore.Samples.Internal;
|
|
using UnityEngine;
|
|
|
|
namespace DCFApixels.DebugXCore.Samples
|
|
{
|
|
[SelectionBase]
|
|
public class DebugXSample_Raycasts2D : MonoBehaviour
|
|
{
|
|
public Gradient Gradient;
|
|
public float GradientMultiplier = 5;
|
|
public Transform[] Points;
|
|
public Transform WarrningPoint;
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
private void OnDrawGizmos()
|
|
{
|
|
Draw();
|
|
}
|
|
#else
|
|
private void Update()
|
|
{
|
|
Draw();
|
|
}
|
|
#endif
|
|
|
|
private void Draw()
|
|
{
|
|
#if DEBUGX_ENABLE_PHYSICS2D
|
|
int i = 0;
|
|
const float RADIUS_M = 0.5f;
|
|
|
|
Transform point;
|
|
Ray ray;
|
|
RaycastHit2D hit;
|
|
|
|
point = Points[i++];
|
|
ray = new Ray(point.position, point.forward);
|
|
hit = Physics2D.Raycast(ray.origin, ray.direction, float.PositiveInfinity, -1);
|
|
DebugX.Draw(GetColor(point)).Raycast2D(ray, hit);
|
|
|
|
point = Points[i++];
|
|
ray = new Ray(point.position, point.forward);
|
|
hit = Physics2D.CircleCast(ray.origin, point.localScale.x * RADIUS_M, ray.direction, float.PositiveInfinity, int.MaxValue);
|
|
DebugX.Draw(GetColor(point)).CircleCast2D(ray, point.localScale.x * RADIUS_M, hit);
|
|
|
|
point = Points[i++];
|
|
ray = new Ray(point.position, point.forward);
|
|
hit = Physics2D.BoxCast(ray.origin, point.localScale, point.eulerAngles.z, ray.direction, float.PositiveInfinity, int.MaxValue);
|
|
DebugX.Draw(GetColor(point)).BoxCast2D(ray, point.eulerAngles.z, point.localScale, hit);
|
|
|
|
point = Points[i++];
|
|
ray = new Ray(point.position, point.forward);
|
|
hit = Physics2D.CapsuleCast(ray.origin, point.localScale, CapsuleDirection2D.Vertical, point.eulerAngles.z, ray.direction, float.PositiveInfinity, int.MaxValue);
|
|
DebugX.Draw(GetColor(point)).CapsuleCast2D(ray, point.eulerAngles.z, point.localScale, CapsuleDirection2D.Vertical, hit);
|
|
#else
|
|
DebugX.Draw(GetColor(WarrningPoint).Inverse()).Text(WarrningPoint.position, "Add \"DEBUGX_ENABLE_PHYSICS2D\" define", DebugXTextSettings.WorldSpaceScale.SetSize(22).SetAnchor(TextAnchor.MiddleCenter));
|
|
#endif
|
|
}
|
|
private Color GetColor(Transform pos1)
|
|
{
|
|
return Gradient.Evaluate(pos1, GradientMultiplier);
|
|
}
|
|
}
|
|
}
|