您当前的位置: 首页 >  unity

莉萝爱萝莉

暂无认证

  • 3浏览

    0关注

    58博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity Line-Renderer

莉萝爱萝莉 发布时间:2021-09-28 09:05:32 ,浏览量:3

绘制线条的组件

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. 附加项
  1. 存在类似的组件,名为trail renderer,可用于绘制拖尾效果
  2. 对每个点添加碰撞器。长度为两个点之间长度,角度设置右方向为该点到下一个点的向量
关注
打赏
1663903574
查看更多评论
立即登录/注册

微信扫码登录

0.0973s