GL

C#实现

不管是画任何东西都需要Begin()和End()函数;

画线

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GL_Line : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. // public Material material;
  8. void OnPostRender()
  9. {
  10. // if(!material){
  11. // Debug.LogError("请给材质赋值??");
  12. // return;
  13. // }
  14. // material.SetPass(0);
  15. GL.LoadOrtho();
  16. GL.Begin(GL.LINES);
  17. GL.Color(new Color(255,0,0));//线段颜色
  18. GL.Vertex(new Vector3(0,0,0));
  19. GL.Vertex(new Vector3(100f/Screen.width,100f/Screen.height,0));
  20. GL.End();
  21. }
  22. }

画曲线

根据鼠标位置实时画线

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GL_Curve : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. List<Vector3> lineInfo;
  8. void Start(){
  9. lineInfo=new List<Vector3>();
  10. }
  11. void Update() {
  12. // if(Input.GetMouseButton(0)) // 添加这句话可以控制开始 结束
  13. lineInfo.Add(new Vector3(Input.mousePosition.x/Screen.width,Input.mousePosition.y/Screen.height,0));
  14. }
  15. void OnPostRender()
  16. {
  17. GL.LoadOrtho();
  18. GL.Begin(GL.LINES);
  19. GL.Color(new Color(255,0,0));
  20. for(int i = 0;i<lineInfo.Count-1;i++){
  21. GL.Vertex(lineInfo[i]);
  22. GL.Vertex(lineInfo[i+1]);
  23. }
  24. //GL.Vertex(new Vector3(100,100,0));
  25. GL.End();
  26. }
  27. }

画正方体

需要准备四个点,位置都是浮点数

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GL_Quads : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. private void OnPostRender() {
  8. GL.LoadOrtho();
  9. GL.Begin(GL.QUADS);
  10. GL.Color(new Color(255,0,0));
  11. GL.Vertex(new Vector3(0,0,0));
  12. GL.Vertex(new Vector3(300f/Screen.width,0,0));
  13. GL.Vertex(new Vector3(300f/Screen.width,300f/Screen.height,0));
  14. GL.Vertex(new Vector3(0,300f/Screen.height,0));
  15. GL.End();
  16. }
  17. }

LineRenderer画线

推荐这篇博客 LineRenderer画线

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GL_Line1 : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. public LineRenderer lineRenderer;
  8. // Update is called once per frame
  9. void Start()
  10. {
  11. lineRenderer.positionCount=4;
  12. lineRenderer.startWidth=0.1f;
  13. lineRenderer.endWidth=0.1f;
  14. }
  15. Vector3 v0=new Vector3(1f,0,0);
  16. Vector3 v1=new Vector3(2f,0,0);
  17. Vector3 v2=new Vector3(3f,0,0);
  18. Vector3 v3=new Vector3(4f,0,0);
  19. private void Update() {
  20. lineRenderer.SetPosition(0,v0);
  21. lineRenderer.SetPosition(1,v1);
  22. lineRenderer.SetPosition(2,v2);
  23. lineRenderer.SetPosition(3,v3);
  24. }
  25. }

unity---GL实现案例的更多相关文章

  1. 转 Unity企业级支持案例与分析

    Unity大中华区技术支持总监张黎明以“Unity企业级支持案例与分析”为主题进行了分享. 以下为演讲实录: 张黎明:非常感谢大家来参加今年的Unite,其实我现在看到有的朋友已经不是第一次来参加Un ...

  2. unite2017《Unity企业级支持案例与分析》

    在今天举办的Unite2017开发者大会上,Unity大中华区技术支持总监张黎明以"Unity企业级支持案例与分析"为主题进行了分享. 以下为演讲实录: 张黎明:非常感谢大家来参加 ...

  3. unity, GL.TexCoord or GL.Color must put before GL.Vertex!!!

    GL.Begin(GL.QUADS);                //in unity, should use left hand rule        //RU        GL.TexCo ...

  4. Unity Dotween官方案例学习

    本文只涉及一些案例,具体查看 DoTween 官方文档. 一. Basics public class Basics : MonoBehaviour { public Transform redCub ...

  5. Unity XLua 官方案例学习

    1. Helloworld using UnityEngine; using XLua; public class Helloworld : MonoBehaviour { // Use this f ...

  6. Unity GL 画圆

    Unity下GL没有画圆的函数,只能自己来了. 如果能帮到大家,我也很高兴. 虽然没有画圆的函数,但是能画直线,利用这一点,配合微积分什么的,就可以画出来了.反正就是花很多连在一起的直线,每条直线足够 ...

  7. Unity GL画折线

    新建一个脚本,这个物体得挂在有摄像机组件的物体上才能生效 OnPostRender() 这个函数才会被自动调用(类似生命周期自动调用) 然后就可以代码画线了,原理是openGL的画线 using Un ...

  8. unity gl 画线

    using UnityEngine; using System.Collections; public class TGLLine : MonoBehaviour { private static M ...

  9. Unity EasyTouch官方案例学习

    一.代码检测手势事件 1. EasyTouch4.x 写法 首先要手动在 Hierarchy 窗口添加 EasyTouch 物体,以触摸(Touch)手势为例,代码如下: using UnityEng ...

  10. Unity设计模式+Java设计模式,讲解+案例+PPT,一次性学会设计模式,拥抱高薪!

    一个程序员对设计模式的理解:“不懂”为什么要把很简单的东西搞得那么复杂.后来随着软件开发经验的增加才开始明白我所看到的“复杂”恰恰就是设计模式的精髓所在,我所理解的“简单”就是一把钥匙开一把锁的模式, ...

随机推荐

  1. 解决联想笔记本 安装VM虚拟机后每次启动都会导致电脑蓝屏问题

    现象描述: pc为联想笔记本 系统是微软家庭中文版,每次启动VM虚拟机都会出现蓝屏现象,出现错误代码system_service_exception 原因及解决方法: 对于Windows10家庭版  ...

  2. 顺序、随机IO和Java多种读写文件性能对比

    概述 对于磁盘的读写分为两种模式,顺序IO和随机IO. 随机IO存在一个寻址的过程,所以效率比较低.而顺序IO,相当于有一个物理索引,在读取的时候不需要寻找地址,效率很高. 基本流程 总体结构 我们编 ...

  3. 手绘模型图带你认识Kafka服务端网络模型

    摘要:Kafka中的网络模型就是基于主从Reactor多线程进行设计的. 本文分享自华为云社区<图解Kafka服务端网络模型>,作者:石臻臻的杂货铺 . Kafka中的网络模型就是基于主从 ...

  4. 设计模式之简单工厂SimpleFactory的实现

    internal interface Chart { void Display(); } internal class LineChart : Chart { public LineChart() { ...

  5. python 反序列化

    Python-反序列化函数使用 pickle.dump(obj, file) : 将对象序列化后保存到文件 pickle.load(file) : 读取文件, 将文件中的序列化内容反序列化为对象 pi ...

  6. Struts2-Action的基本流程

    1.浏览器发送HTTP请求 2.Web容器调用Struts2过滤器的doFilter()方法 3.通过Struts2的内部处理机制,判断HTTP请求是否与某个Action对象匹配 4.如果有与之匹配的 ...

  7. [UE][虚幻]创建默认媒体打包资源路径

    **创建默认媒体打包资源路径** **个人笔记**   **翻阅官方资料,实践出来的!**   **转载,"借鉴",重写...其他行为必须标明出处!!!** UE 官方默认有一个专 ...

  8. 帝国CMS随机调用栏目

    [e:loop={'select * from [!db.pre!]enewsclass where bclassid in(1) order by rand() desc limit 10',10, ...

  9. pycharm的安装指导教程以及破解

    第一步:下载 下载网址:https://www.jetbrains.com/pycharm/download/#section=windows 根据自己的操作系统选择,然后点击download就开始下 ...

  10. IDEA小技巧:Markdown里的命令行可以直接运行了

    作为一名开发者,相信大部分人都喜欢用Markdown来写文章和写文档. 如果你经常用开源项目或者自己维护开源项目,肯定对于项目下的README文件也相当熟悉了吧,通常我们会在这里介绍项目的功能.如何使 ...