如何清除LineRenderer路径重绘线?

问题描述 投票:2回答:1

我有一个LineRenderer路径来显示GolfBall的路径。请在图像中看到栗色路径。

enter image description here

private void createTrail()
{
    lineRenderer.SetColors(tracerColor, tracerColor);
    lineRenderer.SetVertexCount(maxVertexCount);
    for (int idx = 0; idx < (maxVertexCount - 2); idx++)
    {//Add another vertex to show ball's roll
        lineRenderer.SetPosition(idx, new Vector3((float)pts[idx * (int)positionSampling].z, (float)pts[idx * (int)positionSampling].y, (float)pts[idx * (int)positionSampling].x));
    }
    lineRenderer.SetPosition(maxVertexCount - 2, new Vector3((float)pts[goal - 1].z, (float)pts[goal - 1].y, (float)pts[goal - 1].x));
    lineRenderer.SetPosition(maxVertexCount - 1, transform.position);
}

使用pts[] array中的点绘制路径。

在重复显示时,我需要清除旧路径以再次重绘相同的路径。我怎样才能清除旧路?

c# unity3d unityscript unity3d-2dtools
1个回答
3
投票

LineRenderer没有明确的功能,你不需要清除它来重绘它。此外,LineRenderer不需要手动重新绘制。这是由Unity处理的。

如果您的目标是重置设置为它的旧顶点位置,只需将LineRenderer的顶点数设置为0。您可以使用SetVertexCount(0)函数或positionCount变量执行此操作。请注意,SetVertexCount现已弃用。

这应该删除你设置为LineRenderer的所有行:

LineRenderer.positionCount = 0;

扩展方法:

public static class ExtensionMethod
{
    public static void Reset(this LineRenderer lr)
    {
        lr.positionCount = 0;
    }
}

现在,您可以调用lineRenderer.Reset()来重置您设置的所有先前位置/路径。

© www.soinside.com 2019 - 2024. All rights reserved.