Search code examples
graphicsunity-game-engineunityscriptmesh

How to unite gameobject meshes?


Because of I made my house from the unity editor I can't remove this mesh borders. I tried MeshCombiner but it just connects all of the meshes in one mesh borders still exist. I know I can make it from Blender or something like this but is there any one to remove from unity?

Screen Shot: https://i.sstatic.net/dbWJN.jpg


Solution

  • Maybe you are looking for CombineMeshes. An example is

    using UnityEngine;
    using System.Collections;
    
    [RequireComponent(typeof(MeshFilter))]
    [RequireComponent(typeof(MeshRenderer))]
    public class ExampleClass : MonoBehaviour {
        void Start() {
            MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
            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.active = false;
                i++;
            }
            transform.GetComponent<MeshFilter>().mesh = new Mesh();
            transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
            transform.gameObject.active = true;
        }
    }