Unity's screen drawing line

Source: Internet
Author: User
Tags border color

Using Unityengine;
Using System.Collections;

public class Drawrectangle:monobehaviour {

Public Color rectcolor = Color.green;


Private Material Rectmat = null;//Draw line material does not set the system will be the current material draw line results are not controllable

Use this for initialization

void Start () {

Rectmat = new Material ("Shader \" lines/colored blended\ "{" +

"Subshader {Pass {" +

"Blend srcalpha Oneminussrcalpha" +

"Zwrite off cull off Fog {Mode off}" +

"Bindchannels {" +

"Bind \" vertex\ ", vertex bind \" color\ ", color}" +

"}}}");//Create a drawing line material

Rectmat.hideflags = Hideflags.hideanddontsave;

RectMat.shader.hideFlags = Hideflags.hideanddontsave;

}


void Update () {
}



void Onpostrender () {//Draw line This operation is recommended in Onpostrender () rather than directly on update, so a flag is required to open

Rect rect0 = new rect (0,0,0,0);


DrawRect (RECT0);

}

void DrawRect (Rect rect0) {
if (! Rectmat)
Return
Gl. Pushmatrix ();//Save Camera Transform matrix
Rectmat.setpass (0);

Gl. Loadpixelmatrix ();//set to draw with screen coordinates

Gl. Begin (GL. QUADS);

Gl. Color (new color (rectcolor.r,rectcolor.g,rectcolor.b,0.1f));//Set color and transparency, transparent inside the box

Gl. Vertex3 (0,0,0);

Gl. Vertex3 (screen.width/2,0,0);

Gl. Vertex3 (screen.width/2,screen.height/2,0);

Gl. Vertex3 (0,screen.height/2,0);

Gl. End ();
float StartX = rect0.x;
float starty = rect0.y;
float endx = Rect0.xmax;
float EndY = Rect0.ymax;

GL. Begin (GL. LINES);

GL. Color (rectcolor);//Set box border color border opaque

GL. Vertex3 (startx,starty,0);
GL. Vertex3 (endx,starty,0);

GL. Vertex3 (endx,starty,0);
GL. Vertex3 (endx,endy,0);

GL. Vertex3 (endx,endy,0);
GL. Vertex3 (startx,endy,0);

GL. Vertex3 (startx,endy,0);
GL. Vertex3 (startx,starty,0);

GL. End ();
//GL. Begin (GL. LINES);
//GL. Vertex3 (0, 0, 0);
//GL. Vertex3 (screen.width, screen.height, 0);
//GL. End ();
GL. Popmatrix ();//restore Camera projection matrix
}


void Drawrects (rect[] rects) {
if (! Rectmat)
Return
Gl. Pushmatrix ();//Save Camera Transform matrix
Rectmat.setpass (0);

Gl. Loadpixelmatrix ();//set to draw with screen coordinates

Gl. Begin (GL. QUADS);

Gl. Color (new color (rectcolor.r,rectcolor.g,rectcolor.b,0.1f));//Set color and transparency, transparent inside the box

Gl. Vertex3 (0,0,0);

Gl. Vertex3 (screen.width/2,0,0);

Gl. Vertex3 (screen.width/2,screen.height/2,0);

Gl. Vertex3 (0,screen.height/2,0);

Gl. End ();
Gl. Begin (GL. LINES);
for (int i = 0; i < rects. Length; i++) {

Rect rect0 = rects[i];
Debug.Log (RECT0);
float StartX = rect0.x;
float starty = rect0.y;
float endx = Rect0.xmax;
float EndY = Rect0.ymax;



Gl. Color (rectcolor);//Set box border color border opaque

Gl. Vertex3 (startx,starty,0);
Gl. Vertex3 (endx,starty,0);

Gl. Vertex3 (endx,starty,0);
Gl. Vertex3 (endx,endy,0);

Gl. Vertex3 (endx,endy,0);
Gl. Vertex3 (startx,endy,0);

Gl. Vertex3 (startx,endy,0);
Gl. Vertex3 (startx,starty,0);


}
Gl. End ();
Gl. Begin (GL. LINES);
Gl. Vertex3 (0, 0, 0);
Gl. Vertex3 (screen.width, screen.height, 0);
Gl. End ();
Gl. Popmatrix ();//Recover Camera projection matrix

}
}

Unity's screen drawing 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.