Unity的mesh的indices報錯

今天構造Unity的Mesh的索引數組時遇到了個小問題,在此記錄一下,報錯如下:
Failed setting triangles. Some indices are referencing out of bounds vertices. IndexCount: 9240, VertexCount: 9240
UnityEngine.Mesh:set_triangles(Int32[])
LoadPolyMeshByTile:OnDrawGizmosSelected() (at Assets/LoadPolyMeshByTile.cs:695)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

    mesh.Clear();
    mesh.vertices = vertices.ToArray();
    mesh.triangles = tris.ToArray();//這一行報錯

出現這個問題,需要檢查以下兩點:

  • tris內存放的組成三角形的點的索引,所以元素個數必須是三的倍數。
  • tris裏每一個元素記錄的是vertices裏的點的索引值,所以都必須在範圍[0,vertices.Length)範圍之間

出現這種錯誤一般都是第二種,這個時候就去檢查tris裏面的元素值,取其最大值m,若m >= vertices.Length ,證明確實是索引值越界了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章