Unity画线功能LineRenderer详解附有案例
一、LineRenderer是什么
1.含义
LineRenderer是Unity提供的一个用于画线的组件
2.作用
在场景中绘制线段
3.使用场景
(1)绘制攻击范围
(2)武器红外线
(3)辅助功能
(4)其它画线功能
二、LineRender参数相关
1.场景编辑模式
(1)左侧添加点
(2)右侧编辑点
2.无编辑操作
(1)Simplify Preview
简化预览
(2)Tolerance
宽容度(偏离值),越大,偏差越大
3.编辑点模式
(1)Show Wireframe
显示线框
(2)Subdivide Selected
细分选项,选择两个或者多个相邻点时,该按钮启用,会在相邻点之间插入一个新点
4.添加点模式
(1)Input输入模式——Mouse Position
鼠标位置
(2)Input输入模式——Physics Raycast
基于物理射线
(3)Min VertexDistance
最小顶点距离,拖动鼠标穿件点时,会在超出该距离时创建一个点
(4)Offset
偏移量
5.参数理解一
(1)Loop
是否终点起始自动相连
(2)Positions
线段的点
(3)线段宽度曲线调整
(4)Color
颜色变化
(5)Corner Vertices(角定点和圆角)
此属性指示在一条线中绘制角时使用了多少额外的顶点
(6)End Cap Vertices(终端顶点和圆角)
终点圆角
(7)Alignment 对齐方式
View :视点,线段对着摄像机
Transform Z:线段面向其Z轴
(8)Texture Mode 纹理模式
Stretch:拉伸,沿整条线映射纹理一次
Tile:瓷砖平铺,不停地重复纹理
Distribute Per Segment:分配执行
Repeat Per Segment:重复显示
6.参数理解二
(1)Shadow Bias
阴影偏移
(2)Generate Lighting Data
生成光源数据
(3)Use World Space
是否使用世界坐标系
(4)Materials
线使用的材质球
(5)Lighting 光照影响
Cast Shadows:是否开启阴影
Receive Shadows:接收阴影
(6)Probes 光照探针
【1】Light Probes 光探测器模式
Off:不使用光探针
Blend Probes:使用内插光探针
Use Proxy Volume:使用三维网络内插光探针
Custom Provided:自定义从材质决定
【2】Reflection Probes 反射探测器模式
Off:不使用反射探针
Blend Probes:使用混合反射探针
Blend Probes And Skybox:启用混合反射探针并且和天空和混合
Simple:启用普通探针,重叠式不混合
7.参数理解三
(1)Motion Vectors 运动矢量
Camera Motion Only:使用相机运动来跟踪运动
Per Object Motion:同特定对象来跟踪运动
Force No Motion:不跟踪
(2)Dynamic Occludee:动态遮挡剔除
(3)Sorting Layer:排序图层
(4)Order in Layer:线段在排序图层中的顺序
三、LineRender代码相关
1.动态添加一个线段
GameObject line = new GameObject();line.name = "Line";LineRenderer lineRenderer = line.AddComponent<LineRenderer>();
2.首尾相连
lineRenderer.loop = true;
3.开始结束宽
lineRenderer.startWidth = 0.02f;
lineRenderer.endWidth = 0.02f;
4.开始结束颜色
lineRenderer.startColor = Color.white;
lineRenderer.endColor = Color.red;
5.设置材质
m = Resources.Load<Material>("M");lineRenderer.material = m;
6.设置点
(1)先设置点的个数
lineRenderer.positionCount = 4;
(2)再设置对应每个点的位置
lineRenderer.SetPositions(new Vector3[] { new Vector3(0,0,0),new Vector3(0,0,5),new Vector3(5,0,5)});
lineRenderer.SetPosition(3, new Vector3(5, 0, 0));
7.是否使用世界坐标系
决定了 是否随对象移动而移动
lineRenderer.useWorldSpace = false;
8.让线段受光影响
会接受光数据 进行着色器计算
lineRenderer.generateLightingData = true;
四、LineRender练习题
练习题一
1.题目
写一个方法,传入一个中心点,传入一个半径,用LineRender画一个圆出来
2.演示
3.代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class test1 : MonoBehaviour
{private void Start(){DrawLineRenderer(Vector3.zero, 8, 180);}public void DrawLineRenderer(Vector3 centerPos, float r, int pointNum){GameObject obj = new GameObject();obj.name = "Circle";LineRenderer line = obj.AddComponent<LineRenderer>();line.positionCount = pointNum;line.loop = true;float angle = 360f / pointNum;for (int i = 0; i < pointNum; i++){//知识点//1.点加向量 相当于平移点//2.四元数 * 向量 相当于在 旋转向量line.SetPosition(i, centerPos + Quaternion.AngleAxis(angle * i, Vector3.up) * Vector3.forward * r);}}
}
练习题二
1.题目
在Game窗口长按鼠标用LineRenderer画出鼠标移动的轨迹
2.演示
3.代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class test1 : MonoBehaviour
{private LineRenderer line;private Vector3 nowPos;private void Update(){if (Input.GetMouseButtonDown(0)){GameObject obj = new GameObject();line = obj.AddComponent<LineRenderer>();line.loop = false;line.startWidth = 0.5f;line.endWidth = 0.5f;line.positionCount = 0;}if (Input.GetMouseButton(0)){line.positionCount += 1;//知识点//1.如何得到鼠标位置//Input.mousePosition//2.怎么把鼠标 转世界坐标//Camera.main.ScreenToWorldPoint(Input.mousePosition);nowPos = Input.mousePosition;nowPos.z = 10;line.SetPosition(line.positionCount - 1, Camera.main.ScreenToWorldPoint(nowPos));}}
}