Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Help with CombineMeshes.Combine Probuilder function

Discussion in 'World Building' started by demonixis, Mar 26, 2020.

  1. demonixis

    demonixis

    Joined:
    Aug 20, 2013
    Posts:
    116
    Hello,

    I try to merge some meshes at runtime but it doesn't work for me. I've an empty gameobject that have children. Each child has a MeshRenderer and one material. The idea is to merge all these objects into one.

    The first thing to do was to ProBuilderize the meshes, it's done. The second part is to use the Combine function.

    This is my code

    Code (CSharp):
    1.  
    2. var probuilderMeshes = GetComponentsInChildren<ProBuilderMesh>();
    3. var current = gameObject.AddComponent<ProBuilderMesh>();
    4. CombineMeshes.Combine(probuilderMeshes, current);
    There is no error in the console, but the MeshFilter has no mesh and the MeshRenderer has no materials.
    The combine function returns an array of merged meshes, but it also takes in argument the target ProbuilderMesh... So I'm a bit confused by how to use this function correctly.
     
  2. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    258
    If you're using ProBuilder at runtime make sure that in the ProBuilder preferences "Strip ProBuilder Scripts" is disabled.
     
  3. demonixis

    demonixis

    Joined:
    Aug 20, 2013
    Posts:
    116
    Hello,
    I don't have this option in the preference Windows/ProBuilder, is it elsewhere?
     
  4. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    258
    Screen Shot 2020-03-30 at 4.33.35 PM.png

    Make sure this is toggled off.
     
  5. demonixis

    demonixis

    Joined:
    Aug 20, 2013
    Posts:
    116
    I've unchecked it, but it doesn't combine meshes. This is what I tried:

    - Empty GameObject
    - Mesh Renderer (child)
    - Mesh Renderer (child)
    - Mesh Renderer (child)

    I use the following script


    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.ProBuilder;
    4. using UnityEngine.ProBuilder.MeshOperations;
    5.  
    6. namespace TES3Unity.Graphics
    7. {
    8.     public class ProBuilderize : MonoBehaviour
    9.     {
    10.         [SerializeField]
    11.         private bool m_Merge = false;
    12.  
    13.         private void Start()
    14.         {
    15.             var filters = GetComponentsInChildren<MeshFilter>();
    16.  
    17.             foreach (var filter in filters)
    18.             {
    19.                 var mesh = filter.gameObject.AddComponent<ProBuilderMesh>();
    20.  
    21.                 var importer = new MeshImporter(mesh);
    22.                 importer.Import(filter.sharedMesh);
    23.  
    24.                 filter.sharedMesh = new Mesh();
    25.  
    26.                 mesh.ToMesh();
    27.                 mesh.Refresh();
    28.             }
    29.  
    30.             if (m_Merge)
    31.             {
    32.                 Merge();
    33.             }
    34.         }
    35.  
    36. #if UNITY_EDITOR
    37.         [ContextMenu("Merge")]
    38. #endif
    39.         private void Merge()
    40.         {
    41.             var probuilderMeshes = GetComponentsInChildren<ProBuilderMesh>();
    42.             var current = gameObject.AddComponent<ProBuilderMesh>();
    43.             CombineMeshes.Combine(probuilderMeshes, current);
    44.         }
    45.     }
    46. }
    47.  
    I probably doing something wrong but can't figure out what.
     
unityunity