Unity3d如何使用LineRenderer画线
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要介绍Unity3d如何使用LineRenderer画线,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!LineRenderer线渲染器主要是用于在3D中渲染线段,虽然
千家信息网最后更新 2026年11月03日Unity3d如何使用LineRenderer画线
这篇文章主要介绍Unity3d如何使用LineRenderer画线,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
LineRenderer线渲染器主要是用于在3D中渲染线段,虽然我们也可以使用GL图像库来渲染线段,但是使用LineRenderer我们可以对线段进行更多的操作,例如:设置颜色,宽度等。在这里要注意LineRenderer渲染出的线段的两个端点是3D世界中的点,即他是属于世界坐标(World Point)中的。
LineRenderer是以组件形成存在的,首先我们新建一个空的Game Object,然后我们选择"Component→Effects→Line Renderer",即可为其添加LineRenderer组件了。
其实我们也可以通过脚本来为其添加LineRenderer组件:
LineRenderer lineRenderer = gameObject.AddComponent();
获取LineRenderer组件:
lineRenderer = GetComponent();
【 案例】根据鼠标左击的位置,来持续绘制线段
首先我们在场景中新建一个空的 GameObject,并Reset一下。然后将Script1脚本添加给他。
using UnityEngine; using System.Collections; using System.Collections.Generic; //[RequireComponent(typeof(LineRenderer))] public class DrawLine : MonoBehaviour { private LineRenderer line; private bool isMousePressed; private List pointsList; private Vector3 mousePos; //private Vector3 m_DownCamPos; //private Vector3 m_mouseDownStartPos; //主相机节点下 // Structure for line points struct myLine { public Vector3 StartPoint; public Vector3 EndPoint; }; // ----------------------------------- void Awake() { _Init(); } private void _Init() { if (m_init) return; m_init = true; // Create line renderer component and set its property //line = this.GetComponent(); line = gameObject.AddComponent(); line.material = new Material(Shader.Find("Particles/Additive")); line.SetVertexCount(0); line.SetWidth(0.1f, 0.1f); line.SetColors(Color.green, Color.green); line.useWorldSpace = true; isMousePressed = false; pointsList = new List(); line.sortingLayerName = "Ignore Raycast"; line.sortingOrder = 999; // renderer.material.SetTextureOffset( //m_mainCam = this.GetComponent(); } bool m_init = false; //Camera m_mainCam; // ----------------------------------- void Update() { // If mouse button down, remove old line and set its color to green if (Input.GetMouseButtonDown(1)) { isMousePressed = true; line.SetVertexCount(0); pointsList.RemoveRange(0, pointsList.Count); line.SetColors(Color.green, Color.green); //m_DownCamPos = Camera.main.transform.position; //m_mouseDownStartPos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 1f)); } else if (Input.GetMouseButtonUp(1)) { isMousePressed = false; } //if (Input.GetMouseButton(0) || Input.GetMouseButton(1) || Input.GetMouseButton(2)) { // Vector3 crelpos = Camera.main.transform.position; // for (int i = 0; i < pointsList.Count; i++) { // line.SetPosition(i, crelpos + pointsList[i]); // //line.SetPosition(pointsList.Count - 1, (Vector3)pointsList[pointsList.Count - 1]); // } //} // Drawing line when mouse is moving(presses) if (isMousePressed) { Vector3 crelpos = Camera.main.transform.position; //将鼠标点击的屏幕坐标转换为世界坐标,然后存储到position中 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane + 1f)); Vector3 offsetpos = mousePos - crelpos; //Vector3 hitp = Vector3.zero; //bool ok = GetRayCastZero2DPlanePoint(Camera.main, Camera.main.transform.TransformPoint(0f, 0f, Camera.main.nearClipPlane + 1f).z, out hitp); //mousePos = hitp; if (!pointsList.Contains(offsetpos)) { pointsList.Add(offsetpos); line.SetVertexCount(pointsList.Count); for (int i = 0; i < pointsList.Count; i++) { line.SetPosition(i, crelpos + pointsList[i]); //line.SetPosition(pointsList.Count - 1, (Vector3)pointsList[pointsList.Count - 1]); } if (isLineCollide()) { isMousePressed = false; line.SetColors(Color.red, Color.red); } } } } // ----------------------------------- // Following method checks is currentLine(line drawn by last two points) collided with line // ----------------------------------- private bool isLineCollide() { if (pointsList.Count < 2) return false; int TotalLines = pointsList.Count - 1; myLine[] lines = new myLine[TotalLines]; if (TotalLines > 1) { for (int i = 0; i < TotalLines; i++) { lines[i].StartPoint = (Vector3)pointsList[i]; lines[i].EndPoint = (Vector3)pointsList[i + 1]; } } for (int i = 0; i < TotalLines - 1; i++) { myLine currentLine; currentLine.StartPoint = (Vector3)pointsList[pointsList.Count - 2]; currentLine.EndPoint = (Vector3)pointsList[pointsList.Count - 1]; if (isLinesIntersect(lines[i], currentLine)) return true; } return false; } // ----------------------------------- // Following method checks whether given two points are same or not // ----------------------------------- private bool checkPoints(Vector3 pointA, Vector3 pointB) { return (pointA.x == pointB.x && pointA.y == pointB.y); } // ----------------------------------- // Following method checks whether given two line intersect or not // ----------------------------------- private bool isLinesIntersect(myLine L1, myLine L2) { if (checkPoints(L1.StartPoint, L2.StartPoint) || checkPoints(L1.StartPoint, L2.EndPoint) || checkPoints(L1.EndPoint, L2.StartPoint) || checkPoints(L1.EndPoint, L2.EndPoint)) return false; return ((Mathf.Max(L1.StartPoint.x, L1.EndPoint.x) >= Mathf.Min(L2.StartPoint.x, L2.EndPoint.x)) && (Mathf.Max(L2.StartPoint.x, L2.EndPoint.x) >= Mathf.Min(L1.StartPoint.x, L1.EndPoint.x)) && (Mathf.Max(L1.StartPoint.y, L1.EndPoint.y) >= Mathf.Min(L2.StartPoint.y, L2.EndPoint.y)) && (Mathf.Max(L2.StartPoint.y, L2.EndPoint.y) >= Mathf.Min(L1.StartPoint.y, L1.EndPoint.y)) ); } } 以上是"Unity3d如何使用LineRenderer画线"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
线段
组件
世界
坐标
内容
更多
篇文章
脚本
鼠标
两个
价值
位置
兴趣
可以通过
图像
场景
宽度
小伙
小伙伴
屏幕
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
咖啡豆
精品咖啡豆
云之行互联网科技
互联网科技工作委员会毛军
互联网科技保险
互联网中国科技
关于新基建和互联网科技
互联网 科技公司
青岛互联网科技公司
智网互联网科技
奇酷互联网科技
贵州叶脉互联网科技有限公司所属行业
互联网科技革命什么开始
互联网与医疗科技
注册互联网科技公司经营范围大全
中国互联网十大科技股排名
天津腾达互联网科技有限公司
互联网科技和法律行业
特约风豹互联网科技为什么扣费
容联科技互联网电商
手冲咖啡和咖啡机区别
互联网科技股
互联网和高科技公司
互联网时代科技改变生活
湖南诚度互联网科技有限公司
湖北码尼互联网科技有限公司电话
崇明区互联网教育科技售后服务
中国互联网有什么科技
半导体行业属于互联网高科技类吗
南京一网一红互联网科技有限公司