为什么我在画线时不断超出范围?

时间:2018-04-20 06:50:14

标签: c# unity3d

private void Start()
{
    Mesh meshprefab = meshPrefab.GetComponent<MeshFilter>().sharedMesh;
    newVertices = meshprefab.vertices;

    for (int i = 0; i < newVertices.Length; i++)
    {
        DrawLine(newVertices[counter], newVertices[counter + 1], Color.red);
        DrawLine(newVertices[counter + 1], newVertices[counter + 2], Color.red);
        DrawLine(newVertices[counter + 2], newVertices[counter], Color.red);

        counter = counter + 3;
}

例外是在线:

DrawLine(newVertices[counter + 1], newVertices[counter + 2], Color.red);

例外:

IndexOutOfRangeException:数组索引超出范围。

这是完整的脚本:

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class MeshGenerator : MonoBehaviour
{
    public GameObject meshPrefab;
    public Vector3[] newVertices;
    public Vector2[] newUV;
    public int[] newTriangles;

    private List<Vector3> verticesList = new List<Vector3>();
    private List<Vector2> uvsList = new List<Vector2>();
    private List<int> trianglesList = new List<int>();
    private int counter = 0;

    private void Start()
    {
        Mesh meshprefab = meshPrefab.GetComponent<MeshFilter>().sharedMesh;
        newVertices = meshprefab.vertices;

        for (int i = 0; i < newVertices.Length; i++)
        {
            DrawLine(newVertices[counter], newVertices[counter + 1], Color.red);
            DrawLine(newVertices[counter + 1], newVertices[counter + 2], Color.red);
            DrawLine(newVertices[counter + 2], newVertices[counter], Color.red);

            counter = counter + 3;
        }
    }

    void DrawLine(Vector3 start, Vector3 end, Color color, float duration = 0.2f)
    {
        GameObject myLine = new GameObject();
        myLine.transform.position = start;
        myLine.AddComponent<LineRenderer>();
        LineRenderer lr = myLine.GetComponent<LineRenderer>();
        lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
        lr.startColor = color;
        lr.endColor = color;
        lr.startWidth = 0.1f;
        lr.endWidth = 0.1f;
        lr.SetPosition(0, start);
        lr.SetPosition(1, end);
    }
}

2 个答案:

答案 0 :(得分:1)

走线

DrawLine(newVertices[counter + 1], newVertices[counter + 2], Color.red);

所以对于i = 0; I&LT; newVertices.Length; ...

当我是最后一个时,它应该如何添加2?还是1呢?

如果有3

,将其缩小
0 ..  draw newVertices[1], newVertices[2]
1 .. draw newVertices[2], newVertices[3] // error there is no 3
2 .. draw newVertices[3], newVertices[4] // error there is no 3 or 4!

答案 1 :(得分:1)

Supouse表示newVertices是一个包含4个位置的数组[3](0,1,2,3)

在你的第一个循环中,你将拥有

i = 0;
newVertices.Length = 4;
counter = 0;

所以你试图这样做:

DrawLine(newVertices[counter], newVertices[counter + 1], Color.red);
DrawLine(newVertices[counter + 1], newVertices[counter + 2], Color.red);
DrawLine(newVertices[counter + 2], newVertices[counter], Color.red);

翻译为:

DrawLine(newVertices[0], newVertices[1], Color.red);
DrawLine(newVertices[1], newVertices[2], Color.red);
DrawLine(newVertices[2], newVertices[0], Color.red);

所以这第一个循环将起作用,因为你没有超过newVertices lenght,即4.但是你的第二个循环会发生什么?

i = 1;
newVertices.Length = 4;
counter = 3;

所以你试图这样做:

DrawLine(newVertices[counter], newVertices[counter + 1], Color.red);
DrawLine(newVertices[counter + 1], newVertices[counter + 2], Color.red);
DrawLine(newVertices[counter + 2], newVertices[counter], Color.red);

翻译为:

DrawLine(newVertices[3], newVertices[4], Color.red);
DrawLine(newVertices[4], newVertices[5], Color.red); --> ERROR!!!!
DrawLine(newVertices[5], newVertices[3], Color.red); --> ERROR!!!!

您的代码超出了范围错误,因为 newVertices [5] 超出了newVertices.Length尺寸。