using System.Collections.Generic; using UnityEngine; namespace DebugDrawEx { public class Line { public Material Mat { get; set; } public Mesh Mesh { get; private set; } public bool DepthTest { get; set; } public float Duration { get; set; } private readonly List _vertices; private readonly List _colors; private readonly List _indices; private static readonly int ZTest = Shader.PropertyToID("_ZTest"); public Line() { _vertices = new List(); _colors = new List(); _indices = new List(); } public void AddLine(Vector3 from, Vector3 to, Color color) { _vertices.Add(from); _vertices.Add(to); _colors.Add(color); _colors.Add(color); _indices.Add(_vertices.Count - 2); _indices.Add(_vertices.Count - 1); Mesh = CreateMesh(); if (Duration != 0) CreateGameObjectLine(from, to, color); } private void CreateGameObjectLine(Vector3 from, Vector3 to, Color color) { var go = new GameObject("line"); go.hideFlags = HideFlags.HideAndDontSave; var lineMonoBehaviour = go.AddComponent(); lineMonoBehaviour.Point1 = from; lineMonoBehaviour.Point2 = to; lineMonoBehaviour.Color = color; lineMonoBehaviour.Duration = Duration; lineMonoBehaviour.DepthTest = DepthTest; } private Mesh CreateMesh() { var newMesh = new Mesh(); newMesh.MarkDynamic(); Mat = new Material(Shader.Find("Hidden/Internal-Colored")); Mat.SetInt(ZTest, DepthTest ? 4 : 0); newMesh.SetVertices(_vertices); newMesh.SetColors(_colors); newMesh.SetIndices(_indices.ToArray(), MeshTopology.Lines, 0); return newMesh; } } }