using UnityEngine;
using System.Collections;

public class DrawRectangle : MonoBehaviour {

public Color rectColor = Color.green;

private Material rectMat = null;//画线的材质 不设定系统会用当前材质画线 结果不可控

// 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 }" +

"} } }" );//生成画线的材质

rectMat.hideFlags = HideFlags.HideAndDontSave;

rectMat.shader.hideFlags = HideFlags.HideAndDontSave;

}

void Update () {
}

void OnPostRender() {//画线这种操作推荐在OnPostRender()里进行 而不是直接放在Update,所以需要标志来开启

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

drawRect(rect0);

}

void drawRect(Rect rect0){
if (! rectMat)
return;
GL.PushMatrix();//保存摄像机变换矩阵
rectMat.SetPass( 0 );

GL.LoadPixelMatrix();//设置用屏幕坐标绘图

// GL.Begin(GL.QUADS);

// GL.Color( new Color(rectColor.r,rectColor.g,rectColor.b,0.1f) );//设置颜色和透明度,方框内部透明

// 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);//设置方框的边框颜色 边框不透明

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();//恢复摄像机投影矩阵
}

void drawRects(Rect[] rects){
if (! rectMat)
return;
GL.PushMatrix();//保存摄像机变换矩阵
rectMat.SetPass( 0 );

GL.LoadPixelMatrix();//设置用屏幕坐标绘图

// GL.Begin(GL.QUADS);

// GL.Color( new Color(rectColor.r,rectColor.g,rectColor.b,0.1f) );//设置颜色和透明度,方框内部透明

// 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);//设置方框的边框颜色 边框不透明

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();//恢复摄像机投影矩阵

}
}

Unity之屏幕画线的更多相关文章

  1. Unity使用GL画线

    脚本需挂在相机上,如果你的脚本,编辑器报错了,Matrix stack full depth reached,加上这个方法试试GL.LoadPixelMatrix(); using System.Co ...

  2. unity3d之在屏幕上画线

    如何在屏幕上画线,简单的代码如下: using UnityEngine; public class Test : MonoBehaviour { void OnGUI() { GL.LoadOrtho ...

  3. android 屏幕上面画线

    作业如下:在android屏幕上面任意画线 package feng.f121.drawline;//本人创建的包名,每人有每人的不同的包 import java.security.PublicKey ...

  4. Android中Path类的lineTo方法和quadTo方法画线的区别

    转载:http://blog.csdn.net/stevenhu_223/article/details/9229337 当我们需要在屏幕上形成画线时,Path类的应用是必不可少的,而Path类的li ...

  5. Unity3D 画线插件 Vectrosity_Simple2DLine

    Vectrosity是一个很方便的画线插件,用它我们可以画出2D,3D,贝塞尔,圆,椭圆等各种线条图案. :链接: http://pan.baidu.com/s/1pJjTFjt 密码: uesn 首 ...

  6. VC动态轨迹画线

    分类: 2.4 线程/图形学2010-04-30 22:14 1878人阅读 评论(0) 收藏 举报 文档null 这是一个绘制直线的简单绘图程序,能过实现动态轨迹画线,在拖动时产生临时线来表示可能画 ...

  7. android中实现在ImageView上随意画线涂鸦

    我实现的思路: 1.继承ImageView类 2.重写onTouchEvent方法,在ACTION_MOVE(即移动时),记录下所经过的点坐标,在ACTION_UP时(即手指离开时,这时一条线已经画完 ...

  8. Delphi下OpenGL2d绘图(03)-画线

    一.前言 画线与画点基本上代码是相同.区别在于glBegin()的参数.绘制的框架代码可以使用 Delphi下OpenGL2d绘图(01)-初始化 中的代码.修改的部份为 Draw 函数的内容. 二. ...

  9. OpenGL进阶演示样例1——动态画线(虚线、实线、颜色、速度等)

            用OpenGL动态绘制线段.事实上非常easy,但到如今为止.网上可參考资料并不多. 于是亲自己主动手写一个函数,方便动态绘制线段.代码例如以下: #include<GL/glu ...

随机推荐

  1. CloudStack全局参数

    {     "listconfigurationsresponse": {         "count": 305,         "config ...

  2. uva140 - Bandwidth

    Bandwidth Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an orderi ...

  3. 【WebForm】Js调用后台C#方法

    因业务的需要,有这么个需求,需要前台的JS传参调用C#后台的方法.现在有这么个方法可以解决,整理如下. 首先,先说一下基本实现,前台用Jquery的ajax将其中的URL后加方法,然后在Data中传递 ...

  4. Codeforces Round #290 (Div. 2) D. Fox And Jumping dp

    D. Fox And Jumping 题目连接: http://codeforces.com/contest/510/problem/D Description Fox Ciel is playing ...

  5. ASP.NET方面的一些经典文章收集

    1. 在ASP.NET中执行URL重写 文章地址:https://msdn.microsoft.com/zh-cn/library/ms972974.aspx 2. 在ASP.NET中如何实现和利用U ...

  6. MS WORD 表格自己主动调整列宽,自己主动变美丽,依据内容自己主动调整

    在MS WORD中,当有大量的表格出现时,调整每一个表格的的高和宽和大小将是一件很累的事情,拖来拖去,很耗时间,并且当WORD文档达到300页以上时,调整反应很的慢,每次拖拉线后,须要等待一段时间其才 ...

  7. SaundProgressBar

    https://github.com/eltld/SaundProgressBar

  8. 【JavaScript】【译】编写高性能JavaScript

    英文链接:Writing Fast, Memory-Efficient JavaScript 很多JavaScript引擎,如Google的V8引擎(被Chrome和Node所用),是专门为需要快速执 ...

  9. iOS 开发——实用技术Swift篇&Swift 懒加载(lazy)

    Swift 懒加载(lazy) 在程序设计中,我们经常会使用 * 懒加载 * ,顾名思义,就是用到的时候再开辟空间,比如iOS开发中的最常用控件UITableView,实现数据源方法的时候,通常我们都 ...

  10. IPC——共享内存

    Linux进程间通信——使用共享内存 下面将讲解进程间通信的另一种方式,使用共享内存.   一.什么是共享内存 顾名思义,共享内存就是允许两个不相关的进程访问同一个逻辑内存.共享内存是在两个正在运行的 ...