using System.Collections.Generic;
using System;
namespace UnityEngine.XR.Interaction.Toolkit.Utilities
{
///
/// A utility class for visualizing debug lines in VR environments.
///
[AddComponentMenu("")] // Hide in menu
[HelpURL(XRHelpURLConstants.k_XRDebugLineVisualizer)]
internal class XRDebugLineVisualizer : MonoBehaviour
{
///
/// Represents a debug line with its properties.
///
[Serializable]
class DebugLine
{
///
/// The name identifier for the debug line.
///
public string name;
///
/// The color of the debug line.
///
public Color color;
///
/// The LineRenderer component used to render the debug line.
///
public LineRenderer lineRenderer;
///
/// The remaining time before the line decays and is removed.
///
public float decayTime;
}
///
/// List of active debug lines.
///
List m_DebugLines = new List();
///
/// See .
///
void Update()
{
for (int i = m_DebugLines.Count - 1; i >= 0; i--)
{
m_DebugLines[i].decayTime -= Time.deltaTime;
if (m_DebugLines[i].decayTime <= 0f)
{
Destroy(m_DebugLines[i].lineRenderer.gameObject);
m_DebugLines.RemoveAt(i);
}
}
}
///
/// See .
///
void OnDestroy()
{
ClearLines();
}
///
/// Updates an existing debug line or creates a new one if it doesn't exist.
///
/// The name identifier for the line.
/// The start position of the line.
/// The end position of the line.
/// The color of the line.
/// The time in seconds before the line decays and is removed. Defaults to 0.2 seconds.
public void UpdateOrCreateLine(string lineName, Vector3 start, Vector3 end, Color color, float decayTime = 0.2f)
{
DebugLine line = m_DebugLines.Find(l => l.name == lineName);
if (line == null)
{
GameObject lineObj = new GameObject(lineName + "Line");
lineObj.transform.SetParent(transform, false);
LineRenderer lineRenderer = lineObj.AddComponent();
lineRenderer.startWidth = 0.01f;
lineRenderer.endWidth = 0.01f;
lineRenderer.material = new Material(Shader.Find("Sprites/Default"));
lineRenderer.startColor = color;
lineRenderer.endColor = color;
line = new DebugLine { name = lineName, color = color, lineRenderer = lineRenderer, };
m_DebugLines.Add(line);
}
line.lineRenderer.SetPosition(0, start);
line.lineRenderer.SetPosition(1, end);
line.decayTime = decayTime;
}
///
/// Clears all active debug lines.
///
public void ClearLines()
{
foreach (var line in m_DebugLines)
{
Destroy(line.lineRenderer.gameObject);
}
m_DebugLines.Clear();
}
}
}