找回密码
 立即注册
查看: 181|回复: 0

Unity 屏幕绘制

[复制链接]
发表于 2022-12-19 18:56 | 显示全部楼层 |阅读模式

111.gif

/** * ========================================== * FileName: TestDrawLine.cs * Author:   #Author# * CreatTime:#CreateTime# * ========================================== */using System.Collections;using System.Collections.Generic;using System.IO;using System.Xml.Serialization;using UnityEngine;//注意脚本需要挂载到摄像机上面public class TestDrawLine : MonoBehaviour{    Vector3 curPos;    List<List<Vector3>> lineData = new List<List<Vector3>>();    List<Vector3> posList = new List<Vector3>();    int interval;    //public Transform cube;    public Material lineMaterial;    void CreateLineMaterial()    {        if (!lineMaterial)        {            Shader shader = Shader.Find("Unlit/ColorShader");            //Shader shader = Shader.Find("Hidden/Internal-Colored");            lineMaterial = new Material(shader);            //lineMaterial.hideFlags = HideFlags.HideAndDontSave;            ////设置参数            //lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);            //lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);            ////设置参数            //lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);            ////设置参数            //lineMaterial.SetInt("_ZWrite", 0);        }    }    private void OnPostRender()    {        DrawLine();    }    void OnGUI()    {        Event e = Event.current;        if (e != null)        {            if (e.type == EventType.MouseDown)            {                            }            if (e.type == EventType.MouseDrag)            {                if (Vector3.Distance(curPos, Input.mousePosition) > interval)                {                    curPos = Input.mousePosition;                    Vector3 end = new Vector3(curPos.x / Screen.width, curPos.y / Screen.height, 0);                    posList.Add(end);                }            }            if (e.type == EventType.MouseUp)            {                List<Vector3> l = Clone<List<Vector3>>(posList);                if (l.Count > 0)                {                    lineData.Add(l);                }                posList.Clear();                curPos = Vector3.zero;            }        }    }    void DrawLine()    {        GL.PushMatrix();        GL.LoadOrtho();        if (lineMaterial != null)        {            lineMaterial.SetPass(0);        }        GL.Begin(GL.LINES);        GL.Color(Color.red);        for (int j = 0; j < lineData.Count; j++) {            List<Vector3> line = lineData[j];            for (int i = 0; i < line.Count - 1; I++)            {                Vector3 pos = line[I];                GL.Vertex3(pos.x, pos.y, pos.z);                GL.Vertex3(line[i + 1].x, line[i + 1].y, line[i + 1].z);            }        }        for (int i = 0; i < posList.Count - 1; I++)        {            Vector3 pos = posList[I];            GL.Vertex3(pos.x, pos.y, pos.z);            GL.Vertex3(posList[i + 1].x, posList[i + 1].y, posList[i + 1].z);        }        GL.End();        GL.PopMatrix();    }    public void WithDraw() {        if (lineData.Count > 0) {            lineData.RemoveAt(lineData.Count - 1);        }    }    public static T Clone<T>(T RealObject)    {        using (System.IO.Stream stream = new MemoryStream())        {            XmlSerializer serializer = new XmlSerializer(typeof(T));            serializer.Serialize(stream, RealObject);            stream.Seek(0, SeekOrigin.Begin);            return (T)serializer.Deserialize(stream);        }    }}

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-4-28 13:24 , Processed in 0.089628 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表