Unity GL Draw Polyline

Source: Internet
Author: User
Tags polyline

Create a new script that can be applied to objects that have a camera component

Onpostrender () This function will be automatically called (Similar life cycle automatic call)

Then you can draw the line, the principle is OpenGL line

usingUnityengine;usingSystem.Collections;usingSystem.Collections.Generic;/// <summary>///GL Drawing/// </summary> Public classGldraw:unitynormalsingleton<gldraw> {     PublicTransform p1;  PublicTransform p2; PrivateList<vector2>pointlist; Private BOOLIsOpen; PrivateMaterial Mat; PrivateShader Shader; Private voidStart () {pointlist=NewList<vector2>(); Shader= Shader.find ("Unlit/color"); Mat=NewMaterial (shader); Mat. SetColor ("Main Color", Color.Black); }     Public voidDrawLine (list<Object>list)        {pointlist.clear ();  for(inti =0; I < list. Count; i++) {Vector2 Screenpos=(Vector2) list[i]; Pointlist.add (NewVector2 (screenpos.x, Screen.height-screenpos.y)); }    }     Public voidShowLine (BOOLb) {IsOpen=b; }    voidOnpostrender () {if(!IsOpen) {            return; }        //mat = new Material (Shader.find ("Unlit/color")); //Debug.Log ("call"); //if (!mat)//{        //Debug.logerror ("Assign a material on the Inspector"); //return; //}Gl. Pushmatrix ();//Save current MatirxMat. SetPass (0);//Refresh Current MaterialGl. Loadpixelmatrix ();//Set PixelmatrixGL.        Color (Color.yellow); Gl. Begin (GL.        LINES); //draw 2 times, odd even draw continuous line method         for(inti =0; i < Pointlist.count; i++) {GL. Vertex3 (pointlist[i].x, POINTLIST[I].Y,0); }         for(inti =1; i < Pointlist.count; i++) {GL. Vertex3 (pointlist[i].x, POINTLIST[I].Y,0); }
One-time draw line hair//GL. Vertex3 (0, 0, 0);//Gl. Vertex3 (screen.width, screen.height, 0);
Fixed 2 pip//GL. Vertex3 (p1.position.x, P1.POSITION.Y, 0); //GL. Vertex3 (p2.position.x, P2.POSITION.Y, 0);GL. End (); Gl. Popmatrix ();//before reading the matrix }}

Unity GL Draw Polyline

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.