Unity GL Draw Line

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.