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