unity的mesh和submesh

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

public class NewBehaviourScript : MonoBehaviour
{
    public Vector3[] positions;
    public int[] triangles1;
    public int[] triangles2;
    public int[] triangles3;
    public Material[] materials;
    private Mesh mesh;
    private GameObject go;


    void Start()
    {
        mesh = new Mesh();
        mesh.name = "123";
        mesh.vertices = positions;
        go = new GameObject();
        MeshFilter mf = go.AddComponent<MeshFilter>();
        mf.mesh = mesh;
        MeshRenderer mr = go.AddComponent<MeshRenderer>();
        mesh.subMeshCount = 3;
        mr.materials = materials;
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.A))
        {
            mesh.SetTriangles(triangles1, 0);
        }
        if (Input.GetKeyDown(KeyCode.B))
        {
            mesh.SetTriangles(triangles2, 1);
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
            mesh.SetTriangles(triangles3, 2);
        }
    }
}

unity的mesh和submesh_第1张图片

unity的mesh和submesh_第2张图片

你可能感兴趣的:(Unity)