Search Unity

Mesh Combine Wizard (Free Unity Tool & Source Code)

Discussion in 'Assets and Asset Store' started by Gru, Dec 5, 2016.

  1. Gru

    Gru

    Joined:
    Dec 23, 2012
    Posts:
    138
    Link to public repository: https://bitbucket.org/sirgru/meshcombinewizard .

    What does it do?
    Running the wizard will combine all the meshes on the chosen gameObject and its children that share the same material. If there is more than 1 material in all sub-objects, separate gameObjects will be created so that each object has one mesh with one material unique in the group, and will be parented under a new object. A prefab will be created from the combined gameObject in the root of the Assets folder, where all the newly created merged meshes will be created also. The original will be set inactive in the scene and the combined gameObject will be put in its position.

    How does it work?
    Put the provided script in any folder. In the top menu a new entry will appear ("E.S. Tools/Mesh Combine Wizard"). Picking this option will run the wizard. Pick the object from the scene that you wish to combine and clik Create.

    Benefits?
    • Lowers the amount of draw calls, usually dramatically.
    • Does not need to draw all objects in the same batch regardless of wheter they are on screen or not, as opposed to static batching.
    • Does not compromise workflow - no need to merge modular pieces outside Unity in a 3D tool. Quick iteration time when changing the combined design.
    • Works with combined objects that have more than 64k verts, as opposed to solutions that manually set the verts and UVs.
    • Does not compromise the original object's pivot point, as opposed to similar solutions.
    Known limitations:
    • Does not support objects with multiple materials on submeshes. Such meshes should be split in an external 3D tool to have separate meshes, 1 for each submesh and material pair of the original.
    How to use:
    • Place the script in any folder called Editor.
    • Activate the tool with E.S. Tools / Mesh Combine Wizard

    If you're finding this article helpful, consider our asset Dialogical on the Unity Asset store for your game dialogues.
     
    Last edited: Feb 21, 2018
    ZDestructor, ThinhHB, mbbs1 and 3 others like this.
  2. b4c5p4c3

    b4c5p4c3

    Joined:
    Jan 4, 2013
    Posts:
    537
    Very nice, thank you
     
  3. CDUnityDev

    CDUnityDev

    Joined:
    Jan 31, 2017
    Posts:
    25
    Thank you for sharing this excellent tool.
     
  4. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    2,526
    Just found this today. Nice work, doesn't have the gotcha's of most other mesh combine tools.
     
  5. Doctor06

    Doctor06

    Joined:
    Nov 1, 2014
    Posts:
    31
    This is amazing! spent a whole day looking for this! thank you so much for sharing.
     
  6. Gru

    Gru

    Joined:
    Dec 23, 2012
    Posts:
    138
    No problem, please consider my asset Dialogical on Unity Asset store for your game dialogues.
     
  7. teknic

    teknic

    Joined:
    Oct 18, 2012
    Posts:
    29
    Thanks for this. I modified lines 55 and 57 to include the mesh instance ID. This solves the mesh data overwrite problem when combining multiple object groups which all share the same material:

    Code (CSharp):
    1.             AssetDatabase.CreateAsset(combinedMesh, "Assets/CombinedMeshes_" + combinedMesh.GetInstanceID().ToString() + "_" + materialName + ".asset");
    2.  
    3.             string goName = (materialToMeshFilterList.Count > 1) ? "CombinedMeshes_" + combinedMesh.GetInstanceID().ToString() + "_" + materialName : "CombinedMeshes_" + parentOfObjectsToCombine.name;
     
  8. Gru

    Gru

    Joined:
    Dec 23, 2012
    Posts:
    138
    No problem.
    Yes, the overwrite can happen if the asset with that name already exists in root Assets folder. I remember purposefully leaving it like that since I never leave meshes in the project root folder. I've updated the script with a similar solution.
     
  9. peardez

    peardez

    Joined:
    Jun 27, 2017
    Posts:
    10
    Hi there. I've used your tool in my project and it has been really useful but now aparently is provoking some kind of trouble that prevents compiling. I was hoping if you could suggest somesolution for me. I'm using unity 2017.3.0f3
    Thanks in advance.

    PS: The log error says:
    Assets/MeshCombineWizard.cs(2,7): error CS0246: The type or namespace name `UnityEditor' could not be found. Are you missing an assembly reference?

    Assets/MeshCombineWizard.cs(5,34): error CS0246: The type or namespace name `ScriptableWizard' could not be found. Are you missing an assembly reference?

    Build completed with a result of 'Failed'
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)

    UnityEditor.BuildPlayerWindow+BuildMethodException: 3 errors
    at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer (BuildPlayerOptions options) [0x0020e] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:181
    at UnityEditor.BuildPlayerWindow.CallBuildMethods (Boolean askForBuildLocation, BuildOptions defaultBuildOptions) [0x00065] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:88
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr)
     
  10. peardez

    peardez

    Joined:
    Jun 27, 2017
    Posts:
    10
    Never mind. Simply deleting the tool from the editor fixed the issue. Keep the good work. Thanks!
     
  11. Gru

    Gru

    Joined:
    Dec 23, 2012
    Posts:
    138
    Hi @peardez , it seems you haven't placed the script in a folder called Editor. Either that or you're using the new feature called Assembly Definitions https://docs.unity3d.com/Manual/ScriptCompilationAssemblyDefinitionFiles.html and something is wrong with the setup on your end.

    I have added the instructions at the top to clarify.

    Just wanted to point out you'd get this error for any sort of editor script that is not in an Editor folder, because Unity will try to include all non-editor scripts in the build, and UnityEditor assemblies cannot be included in the build.

    Thanks for reporting!
    If you're finding this article helpful, consider my asset Dialogical on for your game dialogues.
     
    twobob likes this.
  12. zero_null

    zero_null

    Joined:
    Mar 11, 2014
    Posts:
    157
    Hello, Honestly the tool is amazing as it's free (Can't complain) but can you please tell what really happens when I select allot of objects?
    The tool creates mesh in a manner that it's connecting like thorough a line.
     
  13. Gru

    Gru

    Joined:
    Dec 23, 2012
    Posts:
    138
    Hi zero_null,
    Not quite sure I understand the question, but it seems you're getting some kind of artifact. The combining part is handled by the call to https://docs.unity3d.com/ScriptReference/Mesh.CombineMeshes.html so this part is handled by the engine. I suggest other techniques for lowering draw calls, as this can be something connected to the specific model (I see you have other non-problematic merges).
     
  14. Dreamcube017

    Dreamcube017

    Joined:
    Dec 20, 2009
    Posts:
    239
    I tried this out and it seemed like it'd be useful, but upon running the script, it mangled the mesh.

    Before:
    upload_2018-4-30_3-51-51.png

    After:
    upload_2018-4-30_3-53-40.png

    I don't think I did anything wrong. However, I am using Unity 2017.2. In Blender this is all one Mesh, but Unity sees it as many which is why this script could be really useful.

    Thanks in advance!
     
  15. Gru

    Gru

    Joined:
    Dec 23, 2012
    Posts:
    138
    Seems like the same problem from above. Didn't run into it in earlier versions of Unity. It is probable that there is some kind of graphics-related reason I don't know about makes Unity see it as different objects, so this way of combining is inappropriate. I guess you could try to merge submeshes in Blender.
     
  16. omid66

    omid66

    Joined:
    Aug 28, 2017
    Posts:
    2
    @grubarec Thanks so much, it worked like a charm.
     
    Gru likes this.
  17. bigdaddio

    bigdaddio

    Joined:
    May 18, 2009
    Posts:
    215
    This works great. If you have some issue where the meshes are getting mangled or making weird connections, split it up. I have things like I made a combined mesh for some intricate columns. Then I put all my columns in and combine them. So do it in steps and eventually have one mesh.
     
    Gru and twobob like this.
  18. dienat

    dienat

    Joined:
    May 27, 2016
    Posts:
    399
    I tried it using several spheres and they appear connected by a mesh instead just showing the spheres without being connected
    upload_2018-9-18_18-38-5.png
     
  19. Worldmy

    Worldmy

    Joined:
    Jan 12, 2017
    Posts:
    1
    Awesome !! Thanks of tons.:)
     
    Gru likes this.
  20. voxelholic

    voxelholic

    Joined:
    Feb 2, 2016
    Posts:
    5
    I was just about to write the same thing but I found this and it worked perfectly! Thank you for sharing.
     
    Gru likes this.
  21. Revisia

    Revisia

    Joined:
    Jun 7, 2019
    Posts:
    3
    Hello, this looks like a great tool. But it isn't letting me create mesh colliders from the combined mesh.
     
unityunity