要将 List 的点用带颜色的线按顺序串联起来,可以使用 LineRenderer 组件来实现。下面是一个示例脚本代码:

using System.Collections.Generic;
using UnityEngine;

public class DrawLines : MonoBehaviour
{
    public List<Vector3> points;  // 存储点的列表
    public Color lineColor;       // 线的颜色

    private LineRenderer lineRenderer;

    void Start()
    {
        // 获取 LineRenderer 组件
        lineRenderer = GetComponent<LineRenderer>();

        // 设置线的颜色
        lineRenderer.startColor = lineColor;
        lineRenderer.endColor = lineColor;

        // 设置线的宽度
        lineRenderer.startWidth = 0.1f;
        lineRenderer.endWidth = 0.1f;

        // 设置线的顶点数量
        lineRenderer.positionCount = points.Count;

        // 设置线的顶点位置
        lineRenderer.SetPositions(points.ToArray());
    }
}

在 Unity 中,创建一个空物体,然后将该脚本挂载到该物体上。在 Inspector 面板中,将 List 的点按顺序添加到 points 列表中。可以通过拖拽点的位置或者在代码中手动添加点。

可以在 Start() 方法中设置线的颜色、宽度和顶点位置。将 List 转换为数组并使用 SetPositions() 方法设置线的顶点位置。

注意:如果要在 Scene 视图中看到线的效果,需要将 LineRenderer 组件的'Use World Space' 属性设置为 true。

Unity 使用 LineRenderer 组件绘制带颜色线段:List<Vector3> 点的顺序连接

原文地址: https://www.cveoy.top/t/topic/fvFN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录