Skip to content

Instantly share code, notes, and snippets.

@runewake2
Last active November 25, 2022 21:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save runewake2/b4b7bf73485222f7c5a0fe0c91dd1322 to your computer and use it in GitHub Desktop.
Save runewake2/b4b7bf73485222f7c5a0fe0c91dd1322 to your computer and use it in GitHub Desktop.
Custom Unity Line Renderer for drawing gizmo style lines in Unity including play mode. Implemented during this video: https://youtu.be/s926MfazI50
using UnityEngine;
// Recommended to attach this script to the Main Camera instance.
// Will not render in play mode if not attached to Camera.
[ExecuteInEditMode]
public class FlowLines : MonoBehaviour
{
public Color baseColor;
public Material material;
public Transform origin;
public Vector3[] points;
public Color[] colors;
void OnPostRender()
{
RenderLines(points, colors);
}
void OnDrawGizmos()
{
RenderLines(points, colors);
}
void RenderLines(Vector3[] points, Color[] colors)
{
if (!ValidateInput(points, colors))
{
return;
}
GL.Begin(GL.LINES);
material.SetPass(0);
for (int i = 0; i < points.Length; i++)
{
GL.Color(baseColor);
GL.Vertex(origin.position);
GL.Color(colors[i]);
GL.Vertex(points[i]);
}
GL.End();
}
private bool ValidateInput(Vector3[] points, Color[] colors)
{
return points != null && colors != null && points.Length == colors.Length;
}
}
@unmodify
Copy link

unmodify commented Jul 6, 2019

You haven't defined origin, but did in your video

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment