Unity:如何限制行渲染器顶点数

时间:2019-05-23 20:22:25

标签: c# unity3d game-development

我试图在停止之前绘制具有允许的最大顶点的线。但是,当我有最大顶点(例如20)时,如果绘制得较快,则线的长度会比绘制得更慢时的线更远。您将如何限制顶点数量,以使线条在停止之前仅画出一定长度?

public List<Vector3> linePoints;
LineRenderer line;
int lineLength = 20;
Vector3 touchPos;
bool touching = false;

void Awake ()
{
  line = gameObject.GetComponent<LineRenderer>();
  linePoints = new List<Vector3>();

  line.SetVertexCount(0);
  line.SetWidth(0.3f, 0.3f);
  line.SetColors(Color.cyan, Color.cyan);

  line.useWorldSpace = true;
}

void Update ()
{
  if (Input.touchCount >= 1)
  {
    touchPos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    touchPos.z = 0;

    if (!touching)
    {
      line.SetVertexCount(0);
      linePoints.RemoveRange(0, linePoints.Count);
      line.SetColors(Color.cyan, Color.cyan);
      touching = true;
    }
    else
    {
      if (linePoints.Count <= lineLength)
      {
        if (!linePoints.Contains(touchPos))
        {
          linePoints.Add(touchPos);
          line.SetVertexCount(linePoints.Count);
          line.SetPosition((linePoints.Count - 1), (Vector3)linePoints[(linePoints.Count - 1)]);
        }
      }
      else
      {
        touching = false;
      }
    }
  }
  else
  {
    touching = false;
  }
}

无论画多快,线条都应呈现相同的长度。

0 个答案:

没有答案