|
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); } }} |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
×
|