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