欢迎光临散文网 会员登陆 & 注册

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

2021-09-25 10:34 作者:TenderRain-  | 我要投稿

在屏幕绘制矩形框

绘制线条一定要再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();
	}

}


Unity中用GL在屏幕绘制矩形框和将数据保存到csv文件中的评论 (共 条)

分享到微博请遵守国家法律