You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity单个LineRenderer能否创建多条不连续线段?

单个Unity LineRenderer实现多条不连续线段的正确方法

可以用单个LineRenderer创建多条不连续线段,但你用Vector3.positiveInfinity分隔的方法行不通——LineRenderer无法识别无穷大坐标,会直接忽略这类无效点,导致线段连在一起。

正确实现思路

在两段独立线段之间插入重复的终点作为断点,然后通过设置断点处的线宽为0,让两段线段之间的连接部分不可见,从而实现视觉上的断开。这种方法既能用单个LineRenderer管理所有线段,也能通过修改LineRenderer的材质/颜色属性,一次性改变所有线段的颜色。

修改后的完整代码

using System.Collections.Generic;
using UnityEngine;

public class CreateSegments : MonoBehaviour
{
    private GameObject board;
    private List<(Vector3, Vector3)> endPoints;
    private int numLines = 8;
    private float radius = 5;
    private float lineLength = 3;
    private LineRenderer lineRenderer;

    private void Awake()
    {
        board = new GameObject("Board");
        board.transform.SetParent(transform);
        lineRenderer = board.AddComponent<LineRenderer>();
        
        // 基础设置:使用世界空间,设置线宽
        lineRenderer.useWorldSpace = true;
        lineRenderer.startWidth = 0.1f;
        lineRenderer.endWidth = 0.1f;
        // 开启每个点的宽度控制(关键)
        lineRenderer.widthMultiplier = 1f;
        lineRenderer.numCapVertices = 5; // 优化线段端点平滑度
    }

    void Start()
    {
        endPoints = new List<(Vector3, Vector3)>();

        for (int i = 0; i < numLines; i++)
        {
            float angle = i * 360f / numLines;
            float angleInRadians = angle * Mathf.Deg2Rad;

            Vector3 startPos = board.transform.position + new Vector3(
                Mathf.Sin(angleInRadians),
                Mathf.Cos(angleInRadians),
                0f
            ) * (radius - lineLength);

            Vector3 endPos = startPos + new Vector3(
                Mathf.Sin(angleInRadians),
                Mathf.Cos(angleInRadians),
                0f
            ) * lineLength;

            endPoints.Add((startPos, endPos));
        }

        // 计算总位置数:每段2个点 + 段间1个断点(共numLines-1个断点)
        int totalPositions = endPoints.Count * 2 + (endPoints.Count - 1);
        Vector3[] positions = new Vector3[totalPositions];
        float[] widths = new float[totalPositions];

        int posIndex = 0;
        for (int i = 0; i < endPoints.Count; i++)
        {
            var (start, end) = endPoints[i];
            
            // 添加当前线段的起点和终点
            positions[posIndex] = start;
            widths[posIndex] = 0.1f; // 正常线宽
            posIndex++;
            
            positions[posIndex] = end;
            widths[posIndex] = 0.1f; // 正常线宽
            posIndex++;
            
            // 如果不是最后一段,添加断点(重复终点,线宽设为0)
            if (i != endPoints.Count - 1)
            {
                positions[posIndex] = end;
                widths[posIndex] = 0f; // 断点处线宽为0
                posIndex++;
            }
        }

        lineRenderer.positionCount = totalPositions;
        lineRenderer.SetPositions(positions);
        lineRenderer.SetWidths(widths); // 设置每个点的线宽
    }

    // 示例:一键修改所有线段颜色
    public void ChangeAllLinesColor(Color newColor)
    {
        lineRenderer.startColor = newColor;
        lineRenderer.endColor = newColor;
    }
}

关键改动说明

  • 移除了无效的Vector3.positiveInfinity分隔方式,改用重复终点+0线宽实现断点
  • 开启了LineRenderer的逐点宽度控制,通过SetWidths方法设置断点处的线宽为0
  • 添加了ChangeAllLinesColor方法,演示如何一次性修改所有线段的颜色
  • 优化了LineRenderer的基础参数,提升渲染效果

内容的提问来源于stack exchange,提问作者DarwinIcesurfer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 14:47:18