untiy3d网格合并和保存

保存生成的新网格

using UnityEngine;
using System.Collections;
using UnityEditor;


public class EditorTools : MonoBehaviour
{


    [MenuItem("Tools/Save Combine Mesh")]
    public static void SaveMesh()
    {
        Mesh m = Selection.activeGameObject.GetComponent().sharedMesh;
        AssetDatabase.CreateAsset(m, "Assets/cmbMesh.asset");
        AssetDatabase.SaveAssets();
    }
}

合并网格

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


public class MyMeshClass : MonoBehaviour {


    void Start()
    {
        MeshFilter[] meshFilters = GetComponentsInChildren();
        CombineInstance[] combine = new CombineInstance[meshFilters.Length];
        int i = 0;
        while (i < meshFilters.Length)
        {
            combine[i].mesh = meshFilters[i].sharedMesh;
            combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
            meshFilters[i].gameObject.SetActive(false);
            i++;
        }


        transform.GetComponent().mesh = new Mesh();
        transform.GetComponent().mesh.CombineMeshes(combine);
        transform.gameObject.SetActive(true);
       
    }

}

你可能感兴趣的:(unity)