如何清除 LineRenderer 路径以重绘线条?

bat*_*man 3 c# unity-game-engine unity3d-2dtools

我有一个 LineRenderer 路径来显示高尔夫球的路径。请参见图片中的栗色路径。

在此处输入图片说明

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);
}
Run Code Online (Sandbox Code Playgroud)

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

在重复显示时,我需要清除旧路径以重新绘制相同的路径。如何清除旧路径?

Pro*_*mer 7

没有清除函数LineRenderer,您无需清除它即可重新绘制它。此外,LineRenderer不需要手动重新绘制。这是由 Unity 处理的。

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

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

LineRenderer.positionCount = 0;
Run Code Online (Sandbox Code Playgroud)

为此的扩展方法:

public static class ExtensionMethod
{
    public static void Reset(this LineRenderer lr)
    {
        lr.positionCount = 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

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