GL

C#实现

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

画线

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class GL_Line : MonoBehaviour
{
// Start is called before the first frame update
// public Material material;
void OnPostRender()
{
// if(!material){
// Debug.LogError("请给材质赋值??");
// return;
// }
// material.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES); GL.Color(new Color(255,0,0));//线段颜色
GL.Vertex(new Vector3(0,0,0));
GL.Vertex(new Vector3(100f/Screen.width,100f/Screen.height,0)); GL.End();
}
}

画曲线

根据鼠标位置实时画线

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class GL_Curve : MonoBehaviour
{
// Start is called before the first frame update
List<Vector3> lineInfo;
void Start(){
lineInfo=new List<Vector3>();
}
void Update() {
// if(Input.GetMouseButton(0)) // 添加这句话可以控制开始 结束
lineInfo.Add(new Vector3(Input.mousePosition.x/Screen.width,Input.mousePosition.y/Screen.height,0));
}
void OnPostRender()
{
GL.LoadOrtho();
GL.Begin(GL.LINES);
GL.Color(new Color(255,0,0));
for(int i = 0;i<lineInfo.Count-1;i++){
GL.Vertex(lineInfo[i]);
GL.Vertex(lineInfo[i+1]);
} //GL.Vertex(new Vector3(100,100,0)); GL.End();
} }

画正方体

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class GL_Quads : MonoBehaviour
{
// Start is called before the first frame update
private void OnPostRender() {
GL.LoadOrtho();
GL.Begin(GL.QUADS);
GL.Color(new Color(255,0,0)); GL.Vertex(new Vector3(0,0,0));
GL.Vertex(new Vector3(300f/Screen.width,0,0));
GL.Vertex(new Vector3(300f/Screen.width,300f/Screen.height,0));
GL.Vertex(new Vector3(0,300f/Screen.height,0)); GL.End();
}
}

LineRenderer画线

推荐这篇博客 LineRenderer画线

using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class GL_Line1 : MonoBehaviour
{
// Start is called before the first frame update
public LineRenderer lineRenderer; // Update is called once per frame
void Start()
{
lineRenderer.positionCount=4;
lineRenderer.startWidth=0.1f;
lineRenderer.endWidth=0.1f; }
Vector3 v0=new Vector3(1f,0,0);
Vector3 v1=new Vector3(2f,0,0);
Vector3 v2=new Vector3(3f,0,0);
Vector3 v3=new Vector3(4f,0,0);
private void Update() {
lineRenderer.SetPosition(0,v0);
lineRenderer.SetPosition(1,v1);
lineRenderer.SetPosition(2,v2);
lineRenderer.SetPosition(3,v3);
}
}

最新文章

  1. mysql 怎么通过sql语句批量去掉某一个表中某一个字段的多余字符
  2. COG注释--转载
  3. php知识案列
  4. C++浅析——继承类中构造和析构顺序
  5. 【POJ 2406】Power Strings 连续重复子串
  6. Shape + Selector: Make a Shape as one item of the Selector
  7. StackOverflow程序员推荐的几本书籍
  8. 数据类型演示DataTypeDemo
  9. mac使用wget下载网站(仿站)
  10. 任务栏窗口和状态图标的闪动 z
  11. 整理的Java资源
  12. My.Ioc 代码示例——谈一谈如何实现装饰器模式,兼谈如何扩展 My.Ioc
  13. iSlider手机平台JS滑动组件
  14. 再见,segmentfault
  15. 自己手动搭建jenkins教程
  16. 微信H5支付证书过滤
  17. SSM商城项目(十三)
  18. windows 控制台下运行cl命令
  19. Java03-Java语法基础(二)运算符
  20. Zookeeper 源码(二)序列化组件 Jute

热门文章

  1. PC端操作系统、移动端操作系统、嵌入式操作系统
  2. H5打造属于自己的视频播放器(JS篇2)
  3. java中抽象类和抽象方法到底有什么用呢?
  4. git总是需要输入用户名密码问题解决
  5. 微信h5支付/jsapi支付/小程序支付
  6. 截取url传值
  7. spring-基于xml的aop开发-快速入门
  8. Gradle 安装记录
  9. css 实现图片专场
  10. 日常使用mobx的小技巧