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. 【官方文档】Hadoop分布式文件系统:架构和设计

    http://hadoop.apache.org/docs/r1.0.4/cn/hdfs_design.html 引言 前提和设计目标 硬件错误 流式数据访问 大规模数据集 简单的一致性模型 “移动计 ...

  2. JS瀑布流布局模式(1)

    在实际的项目中,偶尔会用到一种布局——瀑布流布局.瀑布流布局的特点是,在多列布局时,可以保证内容区块在水平方向上不产生大的空隙,类似瀑布的效果.简单的说,在垂直列表里,内容区块是一个挨着一个的.当内容 ...

  3. Remove a Driver Package from the Driver Store

    http://technet.microsoft.com/en-us/library/cc730875.aspx Determine the name of the driver package in ...

  4. PL/pgSQL学习笔记之十

    http://www.postgresql.org/docs/9.1/static/plpgsql-declarations.html 39.3.3. 类型拷贝 variable%TYPE %TYPE ...

  5. 几种连接数据库的OLEDB驱动程序

    以下是连接几种数据库的驱动,把用"<%"和"%>"括住的地方保存为文件你就可以直接调用了 连接Access数据库 <% dim conn,db ...

  6. C++字节对齐问题

    关于C++字节对齐问题 关于C/C++的字节对齐 这两天写解析SWF文件的程序,在结构体指针和从文件里读出来的进行转换的时候遇到一些问题,就是有一个struct A,比如: struct A { ch ...

  7. Python 基础语法

    Python 基础语法 Python语言与Perl,C和Java等语言有许多相似之处.但是,也存在一些差异. 第一个Python程序 E:\Python>python Python 3.3.5 ...

  8. iOS7与iOS8的比較

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGl1c2h1d2VpMDIyNA==/font/5a6L5L2T/fontsize/400/fill/I0 ...

  9. 使用Url.Routeurl获取url值。

    1,获取url值.         public ActionResult About()         {            RouteValueDictionary RVD = new Ro ...

  10. CF 335A(Banana-贪心-priority_queue是大根堆)

    A. Banana time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...