Unity中用GL在屏幕绘制矩形框和将数据保存到csv文件中

在屏幕绘制矩形框

绘制线条一定要再GL.begin和GL.end之间绘制。两点一线,绘制多边形必须分别绘制每一条线段
线条绘制时需要的两点坐标并不是实际的屏幕上的坐标,而是这个点相对于整个屏幕的比例点,也就是说这个xyz值是0-1之间的值

//创建线条材质
	void CreateLineMaterial()
    {

        if (!lineMaterial)
        {

            // Unity has a built-in shader that is useful for drawing

            // simple colored things.
            Shader shader = Shader.Find("Hidden/Internal-Colored");

            lineMaterial = new Material(shader);

            lineMaterial.hideFlags = HideFlags.HideAndDontSave;

            // Turn on alpha blending

            lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);

            lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);

            // Turn backface culling off

            lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);

            // Turn off depth writes

            lineMaterial.SetInt("_ZWrite", 0);

        }
		else
		{
			lineMaterial.hideFlags = HideFlags.HideAndDontSave;

            // Turn on alpha blending

            lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);

            lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);

            // Turn backface culling off

            lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);

            // Turn off depth writes

            lineMaterial.SetInt("_ZWrite", 0);
		}

    }
    //绘制矩形框
	private void OnGUI() {
		CreateLineMaterial();
		lineMaterial.SetPass(0);
		GL.PushMatrix();
		GL.LoadOrtho();
		GL.Begin(GL.LINES);
        //线条颜色
		GL.Color(Color.blue);

        //绘制当前的矩形框
		GL.Vertex3(MouseDown.x,MouseDown.y,1);
		GL.Vertex3(MouseUp.x,MouseDown.y,1);

		GL.Vertex3(MouseUp.x,MouseDown.y,1);
		GL.Vertex3(MouseUp.x,MouseUp.y,1);

		GL.Vertex3(MouseUp.x,MouseUp.y,1);
		GL.Vertex3(MouseDown.x,MouseUp.y,1);

		GL.Vertex3(MouseDown.x,MouseUp.y,1);
		GL.Vertex3(MouseDown.x,MouseDown.y,1);

        //绘制存在列表中的矩形框
		foreach(var child in RectPos)
		{
			GL.Vertex3(child.StartPos.x,child.StartPos.y,1);
			GL.Vertex3(child.EndPos.x,child.StartPos.y,1);

			GL.Vertex3(child.EndPos.x,child.StartPos.y,1);
			GL.Vertex3(child.EndPos.x,child.EndPos.y,1);

			GL.Vertex3(child.EndPos.x,child.EndPos.y,1);
			GL.Vertex3(child.StartPos.x,child.EndPos.y,1);

			GL.Vertex3(child.StartPos.x,child.EndPos.y,1);
			GL.Vertex3(child.StartPos.x,child.StartPos.y,1);
		}

		GL.End();
		GL.PopMatrix();
	} 

将数据保存至csv文件中
通过StreamWriter类进行写入,编码是否设为UTF8根据需求设置
csv格式是通过英文逗号“,” 进行分割的因此每次输入后如果要再当前行下一列输入需要加上逗号
然后换行需要加上换行符:“\r\n”
写入完毕一定要关闭StreamWriter

public void WriteDatasToCsv()
   {
       //判断是否有文件没有文件时创建文件
       if (!File.Exists(path))
           File.Create(path).Close();

       StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8);
       //开始写入内容
       foreach (var child in SaveRectPos)
       {
           sw.Write(Convert.ToUInt32(child.StartPos.x) + ",");
           sw.Write(Convert.ToUInt32(child.StartPos.y) + ",");
           sw.Write(Convert.ToUInt32(child.EndPos.x) + ",");
           sw.Write(Convert.ToUInt32(child.EndPos.y) + "\r\n");
       }

       sw.Flush();
       sw.Close();
   }

//下面附绘制矩形框和保存到csv的测试用完整脚本代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;


 public class RectPosition
 {
    //左上的点
	public Vector2 StartPos=new Vector2(0,0);
    //右下的点
	public Vector2 EndPos=new Vector2(0,0);
 }
public class RectDataSaveToCsv : MonoBehaviour
{
    public Material lineMaterial=null;
    public Vector3 MouseDown;
    public Vector3 MouseUp;
	public Vector2 SaveMouseDown;
	public Vector2 SaveMouseUp;
    public string path = @"C:\Info.csv";
	List<RectPosition> RectPos=new List<RectPosition>();
	List<RectPosition> SaveRectPos=new List<RectPosition>();
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
		if(Input.GetKeyDown(KeyCode.Mouse0))
		{
			MouseDown=new Vector3(Input.mousePosition.x/Screen.width,(Input.mousePosition.y/Screen.height),0);
			SaveMouseDown=Input.mousePosition;
		}
		if(Input.GetKey(KeyCode.Mouse0))
		{
			MouseUp=new Vector3(Input.mousePosition.x/Screen.width,(Input.mousePosition.y/Screen.height),0);
			SaveMouseUp=Input.mousePosition;
		}
		if(Input.GetKeyUp(KeyCode.Mouse0))
		{
            //保存矩形框数据
			//MouseUp=new Vector3(Input.mousePosition.x/Screen.width,Input.mousePosition.y/Screen.height,0);
			RectPosition rect=new RectPosition();
			rect.StartPos.x=MouseDown.x;
			rect.StartPos.y=MouseDown.y;
			rect.EndPos.x=MouseUp.x;
			rect.EndPos.y=MouseUp.y;
			RectPos.Add(rect);
			MouseDown=MouseUp=new Vector3(0,0,0);

			RectPosition SaveRect=new RectPosition();
			SaveRect.StartPos.x=SaveMouseDown.x;
			SaveRect.StartPos.y=SaveMouseDown.y;
			SaveRect.EndPos.x=SaveMouseUp.x;
			SaveRect.EndPos.y=SaveMouseUp.y;
			SaveRectPos.Add(SaveRect);
			//SaveMouseDown=SaveMouseUp=new Vector2(0,0);
		}
        if (Input.GetKeyDown(KeyCode.Return))
        {
            //将数据保存到CSV文件中
            WriteDatasToCsv();
        }
        if (Input.GetKeyDown(KeyCode.Backspace))
        {
            //删除列表中最后一个矩形框数据
            SaveRectPos.RemoveAt(SaveRectPos.Count-1);
            RectPos.RemoveAt(RectPos.Count - 1);
        }
    }
    public void WriteDatasToCsv()
    {
        //判断是否有文件没有文件时创建文件
        if (!File.Exists(path))
            File.Create(path).Close();

        StreamWriter sw = new StreamWriter(path, true, Encoding.UTF8);
        //开始写入内容
        foreach (var child in SaveRectPos)
        {
            sw.Write(Convert.ToUInt32(child.StartPos.x) + ",");
            sw.Write(Convert.ToUInt32(child.StartPos.y) + ",");
            sw.Write(Convert.ToUInt32(child.EndPos.x) + ",");
            sw.Write(Convert.ToUInt32(child.EndPos.y) + "\r\n");
        }

        sw.Flush();
        sw.Close();
    }

    //创建线条材质
	void CreateLineMaterial()
    {

        if (!lineMaterial)
        {

            // Unity has a built-in shader that is useful for drawing

            // simple colored things.
            Shader shader = Shader.Find("Hidden/Internal-Colored");

            lineMaterial = new Material(shader);

            lineMaterial.hideFlags = HideFlags.HideAndDontSave;

            // Turn on alpha blending

            lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);

            lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);

            // Turn backface culling off

            lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);

            // Turn off depth writes

            lineMaterial.SetInt("_ZWrite", 0);

        }
		else
		{
			lineMaterial.hideFlags = HideFlags.HideAndDontSave;

            // Turn on alpha blending

            lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);

            lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);

            // Turn backface culling off

            lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);

            // Turn off depth writes

            lineMaterial.SetInt("_ZWrite", 0);
		}

    }
    //绘制矩形框
	private void OnGUI() {
		CreateLineMaterial();
		lineMaterial.SetPass(0);
		GL.PushMatrix();
		GL.LoadOrtho();
		GL.Begin(GL.LINES);
        //线条颜色
		GL.Color(Color.blue);

        //绘制当前的矩形框
		GL.Vertex3(MouseDown.x,MouseDown.y,1);
		GL.Vertex3(MouseUp.x,MouseDown.y,1);

		GL.Vertex3(MouseUp.x,MouseDown.y,1);
		GL.Vertex3(MouseUp.x,MouseUp.y,1);

		GL.Vertex3(MouseUp.x,MouseUp.y,1);
		GL.Vertex3(MouseDown.x,MouseUp.y,1);

		GL.Vertex3(MouseDown.x,MouseUp.y,1);
		GL.Vertex3(MouseDown.x,MouseDown.y,1);

        //绘制存在列表中的矩形框
		foreach(var child in RectPos)
		{
			GL.Vertex3(child.StartPos.x,child.StartPos.y,1);
			GL.Vertex3(child.EndPos.x,child.StartPos.y,1);

			GL.Vertex3(child.EndPos.x,child.StartPos.y,1);
			GL.Vertex3(child.EndPos.x,child.EndPos.y,1);

			GL.Vertex3(child.EndPos.x,child.EndPos.y,1);
			GL.Vertex3(child.StartPos.x,child.EndPos.y,1);

			GL.Vertex3(child.StartPos.x,child.EndPos.y,1);
			GL.Vertex3(child.StartPos.x,child.StartPos.y,1);
		}

		GL.End();
		GL.PopMatrix();
	}

} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TenderRain。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值