Draw 2D lines on the screen with the GL class

I’ve seen questions like this asked a million times but still haven’t found an answer. I’m trying to draw discrete 2D lines in on the screen in unity. Here’s an example of what I am after but done with HTML5:

My script is attached to the main camera. Here’s my code so far

    public class DrawStroke : MonoBehaviour {
    	
    	private CheckMouseStatus checkMouseStatus;
    	
    	// vars used for drawing lines with GL
    	public Material mat;
    	private Vector3 startVertex;
    	private Vector3 mousePos;
    	
    	void Start () 
    	{
             checkMouseStatus = GetComponentInChildren<CheckMouseStatus>(); // Get Reference to CheckMouseStatus Script
    	}
    	
    	void Update () 
    	{	
    		mousePos = Input.mousePosition;
    		if (checkMouseStatus.GetMouseStatus() == CheckMouseStatus.Status.down)
    		{
    			startVertex = new Vector3 (mousePos.x / Screen, mousePos.y / Screen.height, 0);
    		}
    		
    	}
    	
    	void OnPostRender ()
    	{
    
    		if (checkMouseStatus.GetMouseStatus() != CheckMouseStatus.Status.notOver) // I check if the mouse is down on a specific game object
    		{
    			
    			GL.PushMatrix();
    			mat.SetPass(0);
    			GL.LoadOrtho ();
    			GL.Begin (GL.LINES);
    			GL.Color (Color.red);
    			GL.Vertex (startVertex);
    			GL.Vertex (new Vector3(mousePos.x / Screen.width, mousePos.y / Screen.height, 0));
    			GL.End ();
    			GL.PopMatrix ();
    		}
    	}
}

This is not working how I would like it to and I am unclear as to how to make the GL class draw discrete lines (and not erase them). I do not want to use a plugin but would love some insight in how to do this with the GL class.

If you are attempting to draw shapes with GL, you can only draw one line at a time. In order to follow the mouse as you move, you will need to store the sequence of mouse positions and draw your lines in a sequence.

I have modified your code below to store up to 2000 vertices. You may want to store fewer vertices, erase the shape upon mouse up, only store the vertices for a limited time, etc. But this should give you a good start:

using UnityEngine;
using System.Collections;

public class DrawStroke : MonoBehaviour {

	private CheckMouseStatus checkMouseStatus;

	// vars used for drawing lines with GL
	public Material mat;
	private Vector3 startVertex;
	private Vector3 mousePos;

	//create an array to store vertices.  Note that it will only store 2001 of them
	private Vector3[] vertexArr = new Vector3[2000];
	//store the last vertex (for comparison to see if the mouse has moved)
	private Vector3 lastVertex = new Vector3(0,0,0);
	//track the number of verteces
	private int vertexCount = 0;

	void Start ()
	{
		checkMouseStatus = GetComponentInChildren<CheckMouseStatus>(); // Get Reference to CheckMouseStatus Script
	}

	void Update ()
	{
		mousePos = Input.mousePosition;
		if (checkMouseStatus.GetMouseStatus() == CheckMouseStatus.Status.down)
		{
			startVertex = new Vector3 (mousePos.x / Screen.width, mousePos.y / Screen.height, 0);

			//if the mouse has moved, add the new position to the vertexArr array
			if (lastVertex != startVertex && vertexCount < 2000){
				vertexArr[vertexCount] = startVertex;
				vertexCount++;
			}
		}
		
	}

	void OnPostRender ()
	{

		if (checkMouseStatus.GetMouseStatus() != CheckMouseStatus.Status.notOver) // I check if the mouse is down on a specific game object
		{
			GL.PushMatrix();
			mat.SetPass(0);
			GL.LoadOrtho ();
			GL.Begin (GL.LINES);
			GL.Color (Color.red);

			//loop through all the vertices in the vertexArr array.
			for (int i=0; i < vertexCount - 1; i++){
				GL.Vertex (vertexArr*);*
  •  	}*
    
  •  	GL.End ();*
    
  •  	GL.PopMatrix ();*
    
  •  }*
    
  • }*

}
For more details, see the answer posted by daybson.paisante here:
Drawing 3D Lines with GL? - Unity Answers