Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Combine meshes in Unity

Discussion in 'Scripting' started by xelvod, Jun 29, 2015.

  1. xelvod

    xelvod

    Joined:
    May 15, 2012
    Posts:
    90
    Hi,
    I have this house made with parts in Unity editor, and i would like to make this house to become like one mesh.
    Found this script but it have problems, some parts become invisible. My house is prefab and static is off.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. [AddComponentMenu("Mesh/Combine Children")]
    5. public class CombineChildren : MonoBehaviour {
    6.  
    7. void Start()
    8. {
    9.   Matrix4x4 myTransform = transform.worldToLocalMatrix;
    10.   Dictionary<Material, List<CombineInstance>> combines = new Dictionary<Material, List<CombineInstance>>();
    11.   MeshRenderer[] meshRenderers = GetComponentsInChildren<MeshRenderer>();
    12.   foreach (var meshRenderer in meshRenderers)
    13.   {
    14.    foreach (var material in meshRenderer.sharedMaterials)
    15.     if (material != null && !combines.ContainsKey(material))
    16.      combines.Add(material, new List<CombineInstance>());
    17.   }
    18.  
    19.   MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
    20.   foreach(var filter in meshFilters)
    21.   {
    22.    if (filter.sharedMesh == null)
    23.     continue;
    24.    CombineInstance ci = new CombineInstance();
    25.    ci.mesh = filter.sharedMesh;
    26.    ci.transform = myTransform * filter.transform.localToWorldMatrix;
    27.    combines[filter.renderer.sharedMaterial].Add(ci);
    28.    filter.renderer.enabled = false;
    29.   }
    30.  
    31.   foreach(Material m in combines.Keys)
    32.   {
    33.    var go = new GameObject("Combined mesh");
    34.    go.transform.parent = transform;
    35.    go.transform.localPosition = Vector3.zero;
    36.    go.transform.localRotation = Quaternion.identity;
    37.    go.transform.localScale = Vector3.one;
    38.  
    39.    var filter = go.AddComponent<MeshFilter>();
    40.    filter.mesh.CombineMeshes(combines[m].ToArray(), true, true);
    41.    var renderer = go.AddComponent<MeshRenderer>();
    42.    renderer.material = m;
    43.   }
    44. }
    45. }
     
  2. willemsenzo

    willemsenzo

    Joined:
    Nov 15, 2012
    Posts:
    585
    First thing that comes to my mind is that your mesh contains contains faces that are backwards.
     
  3. xelvod

    xelvod

    Joined:
    May 15, 2012
    Posts:
    90
    Model looks normal before i attach CombineChildren script.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    think he meant the normals are getting flipped in the combination...