Modify a piece of paper. Hang the script under the camera to display it.
Using Unityengine;
Using System.Collections;
public class joint{
public Vector3 org;
Public Vector3 end;
}
public class Drawline:monobehaviour {
Private Vector3 Orgpos;
Private Vector3 Endpos;
private bool Candrawlines = false;
ArrayList posal;
ArrayList Temppos;
Public Material linematerial;
void Start ()
{
Temppos=new ArrayList ();
Posal=new ArrayList ();
}
void Update ()
{
if (Input.getmousebuttondown (0) | | (Input.touchcount > 0 && input.touches[0].phase = = Touchphase.began))
{
Vector3 Inputpos = Camera.main.ScreenToWorldPoint (input.mouseposition);
RAYCASTHIT2D lasthit = Physics2d.raycast (Inputpos, New Vector2 (0, 0), 0.1f, Layermask.getmask ("Drawline"));
if (Lasthit.collider! = null)
{
Orgpos=input.mouseposition;
Endpos=input.mouseposition;
}
}
else if (Input.getmousebutton (0) | | (Input.touchcount > 0 && input.touches[0].phase = = touchphase.moved))
{
Vector3 Inputpos = Camera.main.ScreenToWorldPoint (input.mouseposition);
RAYCASTHIT2D lasthit = Physics2d.raycast (Inputpos, New Vector2 (0, 0), 0.1f, Layermask.getmask ("Drawline"));
if (Lasthit.collider! = null)
{
Candrawlines = true;
Endpos=input.mouseposition;
Temppos. ADD (input.mouseposition);
Gldrawline (Orgpos,endpos);
Orgpos=input.mouseposition;
}
}
else if (input.getmousebuttonup (0) | | (Input.touchcount > 0 && input.touches[0].phase = = touchphase.ended))
{
Vector3 Inputpos = Camera.main.ScreenToWorldPoint (input.mouseposition);
RAYCASTHIT2D lasthit = Physics2d.raycast (Inputpos, New Vector2 (0, 0), 0.1f, Layermask.getmask ("Drawline"));
if (Lasthit.collider! = null)
{
Endpos=input.mouseposition;
}
}
}
void Gldrawline (Vector3 Beg, Vector3 end)
{
if (!candrawlines)
Return
Gl. Pushmatrix ();
Gl. Loadortho ();
Beg.x=beg.x/screen.width;
End.x=end.x/screen.width;
Beg.y=beg.y/screen.height;
End.y=end.y/screen.height;
Joint tmpjoint = new Joint ();
Tmpjoint.org=beg;
Tmpjoint.end=end;
Posal.add (Tmpjoint);
Linematerial.setpass (0);
Gl. Begin (GL. LINES);
Gl. Color (new color (1,1,1,0.5f));
for (int i= 0;i<posal.count;i++)
{
Joint TJ = (joint) posal[i];
Vector3 Tmpbeg = tj.org;
Vector3 Tmpend=tj.end;
Gl. Vertex3 (TMPBEG.X,TMPBEG.Y,TMPBEG.Z);
Gl. Vertex3 (TMPEND.X,TMPEND.Y,TMPEND.Z);
}
Gl. End ();
Gl. Popmatrix ();
}
public void Clearlines ()
{
Candrawlines = false;
if (posal! = null)
Posal.clear ();
}
void Onpostrender () {
Gldrawline (Orgpos,endpos);
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Unity GL Draw Line