How to draw a line using script

How can I draw a line using script?

void DrawLine(Vector3 start, Vector3 end, Color color, float duration = 0.2f)
{
GameObject myLine = new GameObject();
myLine.transform.position = start;
myLine.AddComponent();
LineRenderer lr = myLine.GetComponent();
lr.material = new Material(Shader.Find(“Particles/Alpha Blended Premultiply”));
lr.SetColors(color, color);
lr.SetWidth(0.1f, 0.1f);
lr.SetPosition(0, start);
lr.SetPosition(1, end);
GameObject.Destroy(myLine, duration);
}

You can either use a LineRenderer (also from a script) for a line drawn on a camera-facing stripe of quads or you can use good'ol GL.Begin(GL.LINES) to have the traditional rasterization to a line of pixels.

A simple one is debug.drawline.

Below from: http://unity3d.com/support/documentation/ScriptReference/Debug.DrawLine.html


Debug.DrawLine

static function DrawLine (start : Vector3, end : Vector3, color : Color = Color.white) : void
Description

Draws a line from the point start to end with color.

The line will be drawn in the scene view of the editor. If gizmo drawing is enabled in the game view, the line will also be drawn there.

// Draws a red line from the the world-space origin to the point (1, 0, 0)
function Update () {
Debug.DrawLine (Vector3.zero, new Vector3 (1, 0, 0), Color.red);
}


Btw, in case you didn't know, a vector3 is a point in space representing x,y,z.

Gizmos.DrawLine() , Debug.DrawLine(),GL.Begin(GL.LINES),or handls.Drawline().

There is a discussion about this here: http://forum.unity3d.com/viewtopic.php?t=34735&sid=b7db7a2f8b373969ba16f82a6af23b4a

The script discussed above is here: http://www.unifycommunity.com/wiki/index.php?title=DrawLine

It might not work for complex drawing cases, but it might work for what you need.

This has been bugging me for ages but I finally found a way. Here is my modified drawing line function which works for me. It is based off the one on the wiki and also includes some commands for drawing in 3D space. Enjoy

using System;
using UnityEngine;
 
public class Drawing
{ 
    public static Texture2D lineTex;
	
	public static void DrawLine(Camera cam, Vector3 pointA, Vector3 pointB) { DrawLine(cam,pointA,pointB, GUI.contentColor, 1.0f); }
	public static void DrawLine(Camera cam, Vector3 pointA, Vector3 pointB, Color color) { DrawLine(cam,pointA,pointB, color, 1.0f); }
	public static void DrawLine(Camera cam, Vector3 pointA, Vector3 pointB, float width) { DrawLine(cam,pointA,pointB, GUI.contentColor, width); }
	public static void DrawLine(Camera cam, Vector3 pointA, Vector3 pointB, Color color, float width){
		Vector2 p1 = Vector2.zero;
		p1.x = cam.WorldToScreenPoint(pointA).x;
		p1.y = cam.WorldToScreenPoint(pointA).y;
		Vector2 p2 = Vector2.zero;
		p2.x = cam.WorldToScreenPoint(pointB).x;
		p2.y = cam.WorldToScreenPoint(pointB).y;
		DrawLine(p1,p2,color,width);
	}
	
    public static void DrawLine(Rect rect) { DrawLine(rect, GUI.contentColor, 1.0f); }
    public static void DrawLine(Rect rect, Color color) { DrawLine(rect, color, 1.0f); }
    public static void DrawLine(Rect rect, float width) { DrawLine(rect, GUI.contentColor, width); }
    public static void DrawLine(Rect rect, Color color, float width) { DrawLine(new Vector2(rect.x, rect.y), new Vector2(rect.x + rect.width, rect.y + rect.height), color, width); }
    public static void DrawLine(Vector2 pointA, Vector2 pointB) { DrawLine(pointA, pointB, GUI.contentColor, 1.0f); }
    public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color) { DrawLine(pointA, pointB, color, 1.0f); }
    public static void DrawLine(Vector2 pointA, Vector2 pointB, float width) { DrawLine(pointA, pointB, GUI.contentColor, width); }
    public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width)
    {
		pointA.x = (int)pointA.x;	pointA.y = (int)pointA.y;
		pointB.x = (int)pointB.x;	pointB.y = (int)pointB.y;
		
		if (!lineTex) { lineTex = new Texture2D(1, 1); }
		Color savedColor = GUI.color;
        GUI.color = color;
		
		Matrix4x4 matrixBackup = GUI.matrix;
		
		float angle = Mathf.Atan2(pointB.y-pointA.y, pointB.x-pointA.x)*180f/Mathf.PI;
		float length = (pointA-pointB).magnitude;
        GUIUtility.RotateAroundPivot(angle, pointA);
        GUI.DrawTexture(new Rect(pointA.x, pointA.y, length, width), lineTex);
		
        GUI.matrix = matrixBackup;
        GUI.color = savedColor;
    }
}

You can try “Dream Paint” ( Asset Store Link ). It allows to draw 2D and 3D primitives in game and in editor, and many others features …

This works if little modified, like this:

using System;
using UnityEngine;
 
public class GUILine
{
 
    public static Texture2D lineTex;
 	
	public static float width=1;
	
    public static void Draw(Vector2 pointA, Vector2 pointB)
    {
        Matrix4x4 matrix = GUI.matrix;
 
        if (lineTex==null) { lineTex = new Texture2D(1, 1); }
 
        float angle = Vector3.Angle(pointB - pointA, Vector2.right);
 
        if (pointA.y > pointB.y) { angle = -angle; }
 		
		GUIUtility.RotateAroundPivot(angle, pointA);

        GUI.DrawTexture(new Rect(pointA.x, pointA.y, (pointA-pointB).magnitude, width), lineTex);
 
        GUI.matrix = matrix;
    }
}`