Unity实现绘制线断一 ————利用LineRenderer组件划线的两种方式
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity实现绘制线断一 ————利用LineRenderer组件划线的两种方式相关的知识,希望对你有一定的参考价值。
这几天,做项目的时候需要用到绘制线的功能,我之前做过划线的功能,总体来说就是三种方式,一种是LineRenderer组件,第二种是GL绘制线,第三种就是Vectrosity插件,他可以绘制各种各图形,
1.首先来说一下LineRenderer组件划线的方式,他需要添加LineRenderer组件,也就需要创建空对象,还的创建Plane,在Plane上画线。
第一种方式:
#region ---- 划线 GameObject line; LineRenderer wire; public Material mline; List<Vector3> p;//一条线上的点的位置 List<List<Vector3>> pos;//若干条线的点的位置 // Use this for initialization void Start() { pos = new List<List<Vector3>>();//对线的初始化 } // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0))//判定是否是画的一条新线 { p = new List<Vector3>();//初始化一条新线 pos.Add(p); //添加到线的数组 line = new GameObject(); wire = line.AddComponent<LineRenderer>(); wire.material = mline; wire.useWorldSpace = false; wire.SetWidth(0.1f, 0.1f); wire.SetColors(Color.yellow, Color.yellow); } //画线 else if (Input.GetMouseButton(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { //存储射线打到palne上面的点 p.Add(hit.point); wire.SetVertexCount(p.Count); //画线遍历 for (int i = 0; i < p.Count; i++) { wire.SetPosition(i, p[i]); } } } } #endregion
第二种方式:
//初始化 public void init () { m_buildUpWall = new GameObject (); m_pointList = new List<Vector3> (); m_outPointList = new List<Vector3> (); m_inPointList = new List<Vector3> (); m_showLineList = new List<ShowLine> (); m_showLinesGojb = new GameObject (); m_wallList = new List<Wall> (); m_showLinesGojb = new GameObject (); m_lineRenderer = m_showLinesGojb.AddComponent<LineRenderer> (); m_lineRenderer.SetWidth (0.01f, 0.01f); // m_showLinesGojb.transform.SetParent (transform); m_buildUpWall.AddComponent<MeshFilter> (); m_buildUpWall.AddComponent<MeshRenderer> (); m_buildUpWall.transform.SetParent (gameObject.transform); m_buildUpWall.transform.localPosition = new Vector3 (-95.6f, -3.3f, -53.3f); m_wallUpNormal = Vector3.up; // m_layoutDesign = new GameObject (); //transform.SetParent (GameObject.Find ("LayoutDesign").transform); m_showPointSphereMat = Resources.Load ("Materials\\BuildwallPointSphere") as Material; } //画线 public void allOtherWallCloseHole () { foreach (Wall wall in m_wallList) { wall.wallInPlane.GetComponent<OpenHoleOnPlane> ().closeHoleNow (); wall.wallOutPlane.GetComponent<OpenHoleOnPlane> ().closeHoleNow (); } } //画线 public void insertNewPoint (Vector3 newPoint) { m_pointList.Add (newPoint); GameObject showPointSphere = GameObject.CreatePrimitive (PrimitiveType.Sphere); showPointSphere.transform.position = newPoint; showPointSphere.transform.localScale = Vector3.one * DrawPointSphereRadius; showPointSphere.GetComponent<Renderer> ().material = m_showPointSphereMat; showPointSphere.transform.SetParent (m_showLinesGojb.transform); Vector3 linePos = newPoint + LineRendererHigherThanGround * Vector3.up; m_lineRenderer.SetVertexCount (m_pointList.Count); m_lineRenderer.SetPosition (m_pointList.Count - 1, newPoint); if (m_pointList.Count > 1) { GameObject lengthText = new GameObject (); float lineLength = (m_pointList [m_pointList.Count - 1] - m_pointList [m_pointList.Count - 2]).magnitude; lengthText.transform.position = (m_pointList [m_pointList.Count - 1] + m_pointList [m_pointList.Count - 2]) / 2; TextMesh textMesh = lengthText.AddComponent<TextMesh> (); textMesh.text = lineLength.ToString("F"); textMesh.alignment = TextAlignment.Center; textMesh.fontSize = 50; textMesh.fontStyle = FontStyle.BoldAndItalic; textMesh.color = Color.red; lengthText.transform.localScale = Vector3.one * 0.04f; lengthText.transform.Rotate(75f, 270f, 0); lengthText.transform.SetParent (m_showLinesGojb.transform); // m_lineRenderer.SetPosition (m_pointList.Count - 1, newPoint); Vector3 lastPoint = m_pointList [m_pointList.Count - 1]; // ShowLine showLine = new ShowLine (); // showLine.start = lastPoint; // showLine.end = newPoint; // m_showLineList.Add (showLine); } }
第一种方式,是绘制曲线,连续的线断,第二种是绘制直线,可画矩形。
GL绘制线断下节我跟大家分享,此次分享希望可以帮助大家
本文出自 “Unity_3D技术探讨” 博客,谢绝转载!
以上是关于Unity实现绘制线断一 ————利用LineRenderer组件划线的两种方式的主要内容,如果未能解决你的问题,请参考以下文章