https://docs.unity3d.com/cn/2019.4/Manual/class-LineRenderer.html
一敏沉、在場景中創(chuàng)建
1.Edit Points in Scene View
添加Line Renderer組件后,點擊+就可以增加點了,點擊左側(cè)的編輯按鈕引润,可以在場景中拖動點的位置规惰。
2.Tolerance Simplify
- Tolerance 設(shè)置簡化線可以偏離原始線的偏差量丹弱。
值為 0 不會導(dǎo)致偏差氮帐,因此幾乎沒有簡化隘截。較高的正值會導(dǎo)致與原始線的偏差更大扎阶,因此更加簡化。
默認值為 1婶芭。 - Simplify 單擊 Simplify 可以減少線渲染器 (Line Renderer) 的 Positions 數(shù)組中的元素數(shù)量东臀。
沒搞懂……
3.部分屬性
Loop 啟用此屬性可連接線的第一個和最后一個位置并形成一個閉環(huán)。
Positions 要連接的 Vector3 點的數(shù)組犀农。
-
Width 定義寬度值和曲線值以控制線沿其長度的寬度惰赋。
曲線是在每個頂點處采樣的,因此其精度受制于線中的頂點數(shù)量呵哨。線的總寬度由寬度值控制赁濒。
Color 定義一個漸變來控制線沿其長度的顏色。
Unity 在每個頂點處從顏色漸變 (Color Gradient) 中采樣顏色孟害。在每個頂點之間流部,Unity 對顏色應(yīng)用線性插值。向線添加更多頂點可能會更接近詳細的漸變纹坐。Corner Vertices 此屬性指示在繪制線中的角時使用多少個額外頂點。增加此值可使線的角顯得更圓舞丛。
End Cap Vertices 此屬性指示使用多少個額外頂點在線上創(chuàng)建端蓋耘子。增加此值可使線的端蓋顯得更圓。
4.Gradient Editor
- 上止點控制的是 Alpha球切,下止點則控制的是顏色谷誓。可以點擊進行新增吨凑,如果需要刪除捍歪,按Delete鍵即可户辱。
- Location:顯示當前所選關(guān)鍵點在漸變上的距離
- 點擊New新建Presets,也可以右擊糙臼,選擇Delete或Rename
二庐镐、代碼創(chuàng)建
全部屬性可以參考https://docs.unity3d.com/cn/2019.4/ScriptReference/LineRenderer.html
1.positionCount 設(shè)置/獲取頂點數(shù)。
https://docs.unity3d.com/cn/2019.4/ScriptReference/LineRenderer-positionCount.html
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour
{
private LineRenderer lr;
void Start()
{
lr = GetComponent<LineRenderer>();
lr.material = new Material(Shader.Find("Sprites/Default"));
// Set some positions
Vector3[] positions = new Vector3[3];
positions[0] = new Vector3(-2.0f, -2.0f, 0.0f);
positions[1] = new Vector3(0.0f, 2.0f, 0.0f);
positions[2] = new Vector3(2.0f, -2.0f, 0.0f);
lr.positionCount = positions.Length;
lr.SetPositions(positions);
}
}
這個例子很簡單变逃,掛在一個添加默認LineRenderer組件的對象上必逆,運行就能看到效果。
PS:可以參考其他屬性中的例子揽乱。
2.參考Unity3d中使用LineRenderer組件畫線
這個例子名眉,需要在場景中添加一個Plane作為背景,腳本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class MsPaint : MonoBehaviour
{
//線條顏色
private Color paintColor = Color.red;
//線條粗細
private float paintSize = 0.1f;
//用來存儲鼠標位置
private List<Vector3> paintPos = new List<Vector3>();
private bool isPressed;//鼠標是否長按
private LineRenderer ren;
private Vector3 lastPos;
//線條材質(zhì)
public Material material;
#region 單選框點擊事件
public void OnRedChange(bool isOn)
{
if (isOn)
{
paintColor = Color.red;
}
}
public void OnGreenChange(bool isOn)
{
if (isOn)
{
paintColor = Color.green;
}
}
public void OnBlueChange(bool isOn)
{
if (isOn)
{
paintColor = Color.blue;
}
}
public void OnPaint1(bool isOn)
{
if (isOn)
{
paintSize = 0.1f;
}
}
public void OnPaint2(bool isOn)
{
if (isOn)
{
paintSize = 0.2f;
}
}
public void OnPaint4(bool isOn)
{
if (isOn)
{
paintSize = 0.4f;
}
}
#endregion
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (EventSystem.current.IsPointerOverGameObject())
{
//判斷是否點擊UI
return;
}
//創(chuàng)建一個空物體凰棉,給他動態(tài)添加LineRenderer組件
GameObject line = new GameObject();
line.transform.SetParent(transform);
ren = line.AddComponent<LineRenderer>();
ren.material = material;//設(shè)置材質(zhì)
ren.startColor = paintColor;//設(shè)置顏色
ren.endColor = paintColor;
ren.startWidth = paintSize;//設(shè)置線的寬度
ren.endWidth = paintSize;
ren.numCapVertices = 2;//設(shè)置端點圓滑度
ren.numCornerVertices = 2;//設(shè)置拐角圓滑度损拢,頂點越多越圓滑
lastPos = GetMousePosition();//獲取鼠標在世界坐標中的位置
paintPos.Add(lastPos);
ren.positionCount = paintPos.Count;//設(shè)置構(gòu)成線條的點的數(shù)量
ren.SetPositions(paintPos.ToArray());
isPressed = true;
}
if (isPressed)
{
//鼠標長按開始繪制,兩點距離大于0.1開始添加點
if (Vector3.Distance(lastPos, GetMousePosition()) > 0.1f)
{
paintPos.Add(GetMousePosition());
lastPos = GetMousePosition();
}
ren.positionCount = paintPos.Count;
ren.SetPositions(paintPos.ToArray());
}
if (Input.GetMouseButtonUp(0))
{
isPressed = false;
paintPos.Clear();
ren = null;
}
}
/// <summary>
/// 獲取鼠標位置,將屏幕坐標轉(zhuǎn)為世界坐標
/// </summary>
/// <returns></returns>
private Vector3 GetMousePosition()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool isCollider = Physics.Raycast(ray, out hit);
if (isCollider)
{
return hit.point - new Vector3(0, 0, 0.01f);//避免遮擋
}
return Vector3.zero;
}
}
三撒犀、填充生成更平滑的點:
https://github.com/gpvigano/EasyCurvedLine
Assets/EasyCurvedLine/Scripts/LineSmoother.cs
四福压、其它參考
深入了解Unity中LineRenderer與TrailRenderer
吹彈牛皮之Unity LineRenderer的簡單應(yīng)用
【Unity】LineRender組件實現(xiàn)閃電效果