当前位置: 首页 > 工具软件 > Mesh Viewer > 使用案例 >

mesh 合并

萧和平
2023-12-01
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

/// <summary>
/// 合并mesh,将要合并的物体放到一个空物体下,选中空物体,点击Plugins/合并选择mesh 出现successful ,点击ok即可
/// </summary>
public class MeshCombine
{
    [MenuItem("Plugins/合并选择mesh")]
    static void CombineMesh()
    {
        GameObject[] objs = Selection.gameObjects;
        for (int j = 0; j < objs.Length; j++)
        {
            MeshFilter[] meshfilters = objs[j].GetComponentsInChildren<MeshFilter>();

            CombineInstance[] combine = new CombineInstance[meshfilters.Length];

            Matrix4x4 matrix = objs[j].transform.worldToLocalMatrix;

            for (int i = 0; i < meshfilters.Length; i++)
            {
                MeshFilter mf = meshfilters[i];

                MeshRenderer mr = mf.GetComponent<MeshRenderer>();

                if (mr == null) continue;

                combine[i].mesh = mf.sharedMesh;

                combine[i].transform = matrix * mf.transform.localToWorldMatrix;
            }
            Mesh mesh = new Mesh();

            mesh.name = objs[j].name;

            mesh.CombineMeshes(combine, true);//合并

            string path = @"Assets/" + mesh.name + ".asset";

            AssetDatabase.CreateAsset(mesh, path);
        }
        AssetDatabase.Refresh();
        EditorUtility.DisplayDialog("CombineMesh", "合并成功!", "OK", "");
    }
}

 

 类似资料: