它绘制了一个三角形但也有例外。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MeshGenerator : MonoBehaviour
{
public GameObject meshPrefab;
public Vector3[] newVertices;
public Vector2[] newUV;
public int[] newTriangles;
private Mesh mesh;
void Start()
{
Mesh meshprefab = meshPrefab.GetComponent<MeshFilter>().sharedMesh;
newVertices = meshprefab.vertices;
newUV = meshprefab.uv;
newTriangles = meshprefab.triangles;
mesh = new Mesh();
mesh = gameObject.GetComponent<MeshFilter>().mesh;
mesh.Clear();
mesh.uv = new Vector2[] { new Vector2(0, 0), new Vector2(0, 0.25f), new Vector2(0.25f, 0.25f) };
mesh.vertices = new Vector3[] { new Vector3(0, 0, 0), new Vector3(0, 0.25f, 0), new Vector3(0.25f, 0.25f, 0) };
mesh.triangles = new int[] { 0, 1, 2 };
}
}
例外是在线:
mesh.uv = new Vector2[] { new Vector2(0, 0), new Vector2(0, 0.25f), new Vector2(0.25f, 0.25f) };
Mesh.uv超出范围。提供的数组需要与Mesh.vertices数组的大小相同。 UnityEngine.Mesh:set_uv(Vector2 [])
答案 0 :(得分:1)
你需要将顶点数组赋值语句移动到uv之前,因为Unity当前认为顶点数是零。
mesh.vertices = ...
mesh.uv = ...