绘制线条的组件
1. 参数列表 参数名描述大小绘制线条关键点的数量,将点依次连接颜色绘制线条的颜色角顶点折点处顶点数量末端顶点末端处顶点数量 2. 代码参数列表// 新建一条线
GameObject go = new GameObject();
go.AddComponent();
参数名描述material材质startWidth起始宽度endWidth末端宽度startColor起始颜色endColor末端颜色numCornerVertices角顶点numCapVertices末端顶点positionCount顶点数量
2. 函数列表
参数名描述SetPositions(Vector3[])顶点集合
3. 画笔示例
// 画笔颜色
[SerializeField] Color paintColor = Color.red;
// 画笔宽度
[SerializeField] float paintSize = 0.2f;
// 画笔材质
[SerializeField] Material lineMaterial;
LineRenderer currentLine;
List positions = new List();
bool IsMouseDown = false;
public void ChangeColor(int value)
{
if(value == 0)
paintColor = Color.red;
if (value == 1)
paintColor = Color.green;
if (value == 2)
paintColor = Color.blue;
}
public void ChangeSize(float pse)
{
paintSize = pse;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
GameObject go = new GameObject();
go.transform.SetParent(this.transform);
currentLine = go.AddComponent();
currentLine.material = lineMaterial;
currentLine.startWidth = paintSize;
currentLine.endWidth = paintSize;
currentLine.startColor = paintColor;
currentLine.endColor = paintColor;
currentLine.numCornerVertices = 5;
currentLine.numCapVertices = 5;
positions.Clear();
AddPosition();
IsMouseDown = true;
}
if(IsMouseDown)
{
AddPosition();
}
if (Input.GetMouseButtonUp(0))
{
IsMouseDown = false;
}
}
void AddPosition()
{
Vector3 position = GetMousePoint();
positions.Add(position);
currentLine.positionCount = positions.Count;
currentLine.SetPositions(positions.ToArray());
}
Vector3 GetMousePoint()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool flag = Physics.Raycast(ray, out hit);
if(flag)
{
return hit.point - Vector3.forward / 100;
}
return Vector3.zero;
}
4. 附加项
- 存在类似的组件,名为trail renderer,可用于绘制拖尾效果
- 对每个点添加碰撞器。长度为两个点之间长度,角度设置右方向为该点到下一个点的向量