Unity合並選中物體的Mesh


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

public class CombineMesh : EditorWindow
{
    [MenuItem("地圖/合並Mesh")]
    static void AddWindow()
    {
        //創建窗口
        CombineMesh window= (CombineMesh)EditorWindow.GetWindow(typeof(CombineMesh), false, "合並Mesh");
        window.Show();

    }
    void OnGUI()
    {
        GUIStyle text_style = new GUIStyle();
        text_style.fontSize = 15;
        text_style.alignment = TextAnchor.MiddleCenter;
        
        if (GUILayout.Button("合並選中物體的Mesh", GUILayout.Height(30)))
        {
            Mesh combineMesh = new Mesh();
            List<MeshFilter> meshlist = new List<MeshFilter>();
            foreach (GameObject m in Selection.gameObjects)
            { 
                    meshlist.Add(m.GetComponent<MeshFilter>());
            }
            CombineInstance[] combine = new CombineInstance[meshlist.Count];
            for(int i = 0; i < meshlist.Count; i++) {
                combine[i].mesh = meshlist[i].sharedMesh;
                combine[i].transform = meshlist[i].transform.localToWorldMatrix;
            }
            GameObject root = new GameObject("root");
            root.AddComponent<MeshRenderer>().sharedMaterial=meshlist[0].GetComponent<MeshRenderer>().sharedMaterial;
            root.AddComponent<MeshFilter>().sharedMesh = new Mesh();
            root.GetComponent<MeshFilter>().sharedMesh.CombineMeshes(combine);
        }
    }


    void OnInspectorUpdate()
    {
        this.Repaint();
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM