LineRenderer基础

LineRenderer线渲染器主要是用于在3D中渲染线段,虽然我们也可以使用GL图像库来渲染线段,但是使用LineRenderer我们可以对线段进行更多的操作,例如:设置颜色,宽度等。在这里要注意LineRenderer渲染出的线段的两个端点是3D世界中的点,即他是属于世界坐标(World Point)中的。 LineRenderer是以组件形成存在的。

一些常用属性:
图一图一
Positions:线的位置(有"Size"个拐点,每个拐点的坐标在哪里,很显然第一个拐点就是起点(0),最后一个就是终点(1),当只有两个拐点时,就是一条线段)
Width:线的粗细(横轴表示时间,数轴表示宽度)
Color:颜色,也有多段

一些简单的操作:
1.新建一个cube,然后添加LineRenderer组件,给LineRenderer组件添加材质,材质上加的图片,当选择材质的类型为"Particles/Additive"时可以去除背景只留下线段。
在这里插入图片描述
2.可以手动设置线段的起点和终点,颜色的渐变。这些在图一中可以设置。也可以在脚本中用代码设置。
3.新建一个球体当做线段的终点。可以在Positions里直接设置。也可以用代码

public class Line : MonoBehaviour
{
    public GameObject ball;
     LineRenderer line;
    private Vector3 position;
    // Start is called before the first frame update
    void Start()
    {
        line = transform.GetComponent<LineRenderer>();
     //   line.material = new Material(Shader.Find("Particles/Additive")); //添加材质
        line.startColor = Color.red;//起点的颜色
        line.endColor = Color.blue;//终点的颜色
       // line.SetColors(Color.blue, Color.red);
    }

    // Update is called once per frame
    void Update()
    {
        line.SetPosition(0, transform.position);//起点为立方体的位置
        line.SetPosition(1,ball.tranform.position);//终点为球体的位置
      //  起点和终点会随着两个物体的移动而移动。

    }
}

4设置一个物体为起点,鼠标点击为终点。新建一个脚本,挂在起点物体上。

public class Line : MonoBehaviour
{
    public GameObject ball;
     LineRenderer line;
    private Vector3 position;
    // Start is called before the first frame update
    void Start()
    {
        line = transform.GetComponent<LineRenderer>();
     //   line.material = new Material(Shader.Find("Particles/Additive")); //添加材质
        line.startColor = Color.red;
        line.endColor = Color.blue;
       // line.SetColors(Color.blue, Color.red);
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //将鼠标点击的屏幕坐标转换为世界坐标,然后存储到position中  
            position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f));
           
        }

        line.SetPosition(0, transform.position);
        line.SetPosition(1,position);

    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值