美文网首页
Unity 屏幕绘制

Unity 屏幕绘制

作者: 114105lijia | 来源:发表于2022-12-19 17:35 被阅读0次
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 屏幕绘制

  • 2020-08-31Unity GL函数库的简单使用

    使用Unity GL库绘制图像 使用Unity自带的GL函数库在屏幕上绘制一些特殊的图形,如圆、线条、曲线、矩形等...

  • Unity Shader 矩阵转换

    在unity中,一个点或者物体要从模型空间绘制到屏幕空间上,需要经过以下空间转换(小白,用词表达可能不准确) 首先...

  • 记录一些好的网站

    在Unity中实现屏幕空间阴影(1)在Unity中实现屏幕空间阴影(2)游戏里的动态阴影-ShadowMap实现原...

  • Unity WebGL 项目, 屏幕自适应

    Unity WebGL 项目, 屏幕自适应 Unity 2017.4.0f1 2018.4.10 如图所示, 使用...

  • 屏幕绘制功能

    需求:点击截图按钮,截取整个ScrollView.contentSize上的内容。 点击右上角截图按钮截图 截图效...

  • iOS屏幕绘制

    屏幕是如何绘制的?这其中涉及到了很多的流程和不同的系统框架,这里就来梳理一边在屏幕绘制的过程中都发生了什么,希望可...

  • Unity屏幕适配

  • Unity屏幕适配

    Author :JerryYangCreate By 2020.11.02 环境:Unity:2019.4.2f1...

  • unity屏幕适配

    屏幕适配:简单说,就是将UI以适当的尺寸显示在屏幕上的适当位置。比如我们在手机上运行一个小游戏,打开它的菜单栏,一...

网友评论

      本文标题:Unity 屏幕绘制

      本文链接:https://www.haomeiwen.com/subject/opueqdtx.html