Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEditor;
- using UnityEngine;
- public class MeshMerger : MonoBehaviour
- {
- public GameObject[] meshesToMerge;
- [ContextMenu("Merge Meshes")]
- void MergeMeshes()
- {
- if (meshesToMerge.Length != 4)
- {
- Debug.LogError("Please assign exactly 5 meshes to merge.");
- return;
- }
- List<CombineInstance> combine = new List<CombineInstance>();
- MeshFilter[] meshFilters = new MeshFilter[meshesToMerge.Length];
- for (int i = 0; i < meshesToMerge.Length; i++)
- {
- meshFilters[i] = meshesToMerge[i].GetComponent<MeshFilter>();
- if (meshFilters[i] == null)
- {
- Debug.LogError($"MeshFilter component not found on GameObject {i}");
- return;
- }
- }
- for (int i = 0; i < meshFilters.Length; i++)
- {
- CombineInstance ci = new CombineInstance();
- ci.mesh = meshFilters[i].sharedMesh;
- ci.transform = meshFilters[i].transform.localToWorldMatrix;
- combine.Add(ci);
- }
- Mesh combinedMesh = new Mesh();
- combinedMesh.CombineMeshes(combine.ToArray());
- GameObject mergedObject = new GameObject("MergedMesh");
- MeshFilter mergedMeshFilter = mergedObject.AddComponent<MeshFilter>();
- mergedMeshFilter.sharedMesh = combinedMesh;
- MeshRenderer mergedMeshRenderer = mergedObject.AddComponent<MeshRenderer>();
- mergedMeshRenderer.sharedMaterial = meshesToMerge[0].GetComponent<MeshRenderer>().sharedMaterial;
- // Save the combined mesh as an asset
- AssetDatabase.CreateAsset(combinedMesh, "Assets/MergedMesh.asset");
- AssetDatabase.SaveAssets();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement