Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Combine Children Extented (sources to share)

Discussion in 'Scripting' started by Neodrop, Dec 26, 2009.

  1. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    At first, sorry for my english. My teachers were Japanese
    :D

    As it is known, combining of the objects using one material in one mesh, strongly raises productivity. For this purpose in Standard Assets Unity there is script Combine Children (further: the Combine)
    In the course of my work, some additives to a standard script of the Combine were created. And I share it:



    [V] Frame To Wait: how many frames on script start to wait before to make combining.
    For example, you have an object containing one hundred more other objects which too contain childs. Prefab of internal objects, it is equipped by the Combine with own customisations. On start, all of them will be combined and we will receive less meshes, but all the same a heap. Why not to Combine them again?
    We put "1" in this setting and in a following frame, after end childs is combined, we stick together new received meshes again and it is received 5 - 10 meshes instead of old hundreds.
    [V] Combine On Start - the script will work on composition start (if it is activated, sure).
    [V] Destroy After Optimized - to kill all initial objects. Cautiously! If on these objects have colliders, they too are deleted.
    [V] Cast Shadow - created mesh will cast shadow.
    [V] Receive Shadow - created mesh will accept shadow.
    [V] Keep Layer - the created object will save the same layer, as for the initial object (on which the script is)



    Well and, a bonus!
    [V] Under the right button there lives Combine Now command. That allows to spend all combination directly in the Editor, to estimate changes and, or to cancel them, or to save in the Scene. That is - to avoid necessity combining on application start.

    UPDATE : WinRT compatible version from Nidre

    UPDATE : New command ("Save mesh as Asset") added. Undo-Redo is supported now. Also, the script renamed to CombineChildrenExtended.cs
     

    Attached Files:

    Last edited: Oct 5, 2013
  2. DrHotbunz

    DrHotbunz

    Joined:
    Feb 14, 2009
    Posts:
    315
    Thanks for sharing.
     
  3. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Great!
    I have made some customisations of my own to that script, but this is even better! :D
     
  4. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359


    I have forgotten to explain one more command. By call of this command, there is a search and execution the Combine Children script on all children, except the object from which the command is started.
     
  5. mattimus

    mattimus

    Joined:
    Mar 8, 2008
    Posts:
    576
    Thanks for this! The ability to combine in editor rather than at start gave me some much needed reduction in start time within one of my iphone apps.

    Just a heads up for others interested in this script, it does cause a few problems in the iphone version. Namely the CallCombineOnAllChilds method. I think it has to do with how the array c is declared. I didn't actually need that functionality, so I just commented out that whole method and everything works beautifully. Maybe someone with a little more iphone-specific knowledge can properly declare the array and regain that funcitonality :)
     
  6. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    I'll check it tomorrow in iPhone. I thing, there is some .NET 2.0 incompatibility.

    But, this is not an absolute method. Sometimes combined mesh is disappears. Be cary in Editor.
     
  7. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    New version uploaded in the first post. A little bit improved.
     
  8. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    i have a lot of meshes in the scene,although they are all in separate GO with some scripts attached. could
    i use this script since it combines meshes, will i be able to use it as i am now? thanks!
     
  9. shinja

    shinja

    Joined:
    Nov 13, 2009
    Posts:
    17
    Hi Neodrop,

    This is a very cool script. Although i have lightmaps on my gameobject that have this script running and my objects totally disappear once the game is executed. I am not a programmer but just wanting to know if anyone has an explanation to why all my game object children meshes disappears and without lightmaps it works ok.
     
  10. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    I saw a similar disappearance with using Unity's Combine function on a laptop. This was displaying absolutely fine on my home PC, but on the laptop, certain precise chars were disappearing.

    I later found that it was causd by my laptop's GPU, which was badly interpreting some of the imported normals.

    Try to put "calculate normals" in your FBX importer on your models, and set it to 60. It completely solved the problem for me.
     
  11. shinja

    shinja

    Joined:
    Nov 13, 2009
    Posts:
    17
    Hi Nomad,

    Thanks heaps for the info i tried doing what you said but it did not work :( i may have to look around more and experiment. I am currently exporting from XSI but it seems like a bug so I will get Unity tech to look at it.
     
  12. 3Duaun

    3Duaun

    Joined:
    Dec 29, 2009
    Posts:
    600
    whoa... I just found this script, Thank you Neodrop! Another very useful script(as is Antares Vizio). The ability to combine in editor is MUCH APPRECIATED!
     
  13. TheCheese

    TheCheese

    Joined:
    Nov 25, 2009
    Posts:
    82
    Holy crap - using this script just helped me a ton - Thanks for a great tool.

    @shinja: You've probably moved on, but your objects are probably disappearing when using lightmaps because UVs are being changed when the new meshes are created on startup.

    I'm guessing you can solve this by using this script's awesome "Combine Now" feature. Combine your objects in the editor, and THEN do your lightmapping and things should be fine.
     
  14. PolyMad

    PolyMad

    Joined:
    Mar 19, 2009
    Posts:
    2,350
    Trying to make this script work but when I try to attach it to an object it says that the filename does not match the class name... and it seems there is no class name definition in the script.
    I am no C# programmer so I cannot solve this problem :|

    By the way, Neodrop, I'll be glad to help you with any project with a couple of models to pay back this precious script :)
     
    Last edited: Mar 31, 2011
  15. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    This is very old script. May be it's is obsolete for Unity 3.0
    In my Antares Project I have new working one. Take it from my signature.
     
  16. synapsemassage

    synapsemassage

    Joined:
    Jul 27, 2009
    Posts:
    334
    Privet Neodrop,

    when combining meshes would it be possible to recombine gameobjects/meshes on runtime? How much of a performance impact would that be? Too much to even think about it? I 'm wondering if it might be an option in an iPhone game to reduce draw calls with meshes that switch materials during gameplay? For example when collision happens collided objects change material, then, based on the material the object is added/removed/combined dynamically.
     
  17. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    You can do it, if you will store source meshes somewhere and use it for recreating/restoring objects state.
    I thing, mostly all will works fine in reltime (I often have used it in realtime). But in Unity 3.x.x we has a static dinamic butching and in most cases I don't need self-combining anymore.
     
  18. synapsemassage

    synapsemassage

    Joined:
    Jul 27, 2009
    Posts:
    334
    Thanks Neodrop!

    Does this mean that I destroy the combined mesh, create a new combined mesh from other source object meshes and recombine these based on their shared material + doing the same for another material? I would like to switch materials of individual childs inside a combined mesh. It would start with one material+one combined mesh ( one draw call) and end with two combined meshes based on two materials ( 2 draw calls)? This is supposed to happen on the fly on a collision event in realtime?

    According to the documentation dynamic batching makes sense only with objects below 300 vertices due to overhead, so only combining or using workaround with bones+mesh makes sense as solution (in my case I have more than 1000 vertices per object).
     
  19. Madgeniy

    Madgeniy

    Joined:
    Apr 21, 2012
    Posts:
    14
    Thanks very much! It's great.
     
  20. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    6,898
    so small, so sweet and so smart: a very useful script in case you want to speed up your game.

    thanks a lot,

    lars
     
  21. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    Well, in general, I just rewrite it a bit from original Unity example (as I remember)
     
  22. kurotatsu

    kurotatsu

    Joined:
    May 10, 2012
    Posts:
    588
    I don't post very often, but having used your script, I just want to take a moment and say thank you.
     
  23. Hakimo

    Hakimo

    Joined:
    Apr 29, 2010
    Posts:
    316
    Does the script still work? It says I don't have the MeshCombine Utility.
     
  24. davebuchhofer

    davebuchhofer

    Joined:
    Nov 9, 2007
    Posts:
    126
    It should, its been a while since I've looked, but the MeshCombine is included in the unity standard scripts I believe
     
  25. ZoomDomain

    ZoomDomain

    Joined:
    Aug 30, 2012
    Posts:
    150
    Thanks! great script. the other version was not working for me because: by creating a procedural mesh content, the Combine Mesh funtion was being called at startup as if the parent object was already full of mesh, i think, in any case it wasnt combining.

    Your version works by letting the wait frames command wait until the procedural mesh is computed or by the context menu left click command.

    i want to figure a way to call the Combine () function in this script to combine parent object from the end of the procedural content creation script. ill post a question in the answers forum under this name TY so much!
     
    Last edited: Sep 18, 2012
  26. Wessmess

    Wessmess

    Joined:
    Aug 8, 2012
    Posts:
    1
    When I run this script, it works some of the time. But sometimes, it creates 2 children to the parent called "Combined mesh". It puts the mesh renderer and mesh collider separately on these 2 children. Therefore, I cannot use the mesh colliders for the parent object.

    What am I doing wrong here? I don't see anyone else having this issue. Like I said, it works great on several of my objects. But on a lot of them, it does this...
     
  27. Xin

    Xin

    Joined:
    Aug 30, 2012
    Posts:
    11
    I am getting the same error, and I don't see the "MeshCombine" anywhere?
     
  28. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    Every "Combined mesh" is created for a every material. Two materals = two combined meshes
     
  29. fred_gds

    fred_gds

    Joined:
    Sep 20, 2012
    Posts:
    184
    Hey,

    I keep getting this error when trying to attach the scirpt to a component

    Can't add script behaviour combinechildren_962. The scripts file name does not match the name of the class defined in the script!

    I'm using unity iphone what do I need to change to get it working?
     
  30. Casper15

    Casper15

    Joined:
    Dec 13, 2012
    Posts:
    3
    hi somone can say me were i can find a simple control script??
     
  31. fred_gds

    fred_gds

    Joined:
    Sep 20, 2012
    Posts:
    184
    for what? there is a character system in unity
     
  32. QuantumTheory

    QuantumTheory

    Joined:
    Jan 19, 2012
    Posts:
    1,081
    Hi Xin,

    Simply rename the script file to "CombineChildren.cs" and it will work fine.

    Also, the meshcombineutility is found when you do "Import Package -> Scripts"
     
  33. Bunzaga

    Bunzaga

    Joined:
    Jan 9, 2009
    Posts:
    202
    Thank you for making this and especially for putting it out into the public.
     
  34. mastermax001

    mastermax001

    Joined:
    Aug 9, 2012
    Posts:
    9
    hi folks,

    anybody have tried this script for unity 4.x?
     
  35. aroha

    aroha

    Joined:
    Jan 8, 2013
    Posts:
    18
    I tested it on Unity 4.1 and it was working okay.

    But I needed a single mesh with multiple materials so I slightly modified a script on the answers forum I found and posted it here.
     
  36. keinabel

    keinabel

    Joined:
    Nov 11, 2012
    Posts:
    1
    using either your version or the one from unity's standart scripts always turn my gameobjects invisible...
    You know why this happens?
     
  37. augasur

    augasur

    Joined:
    Mar 4, 2012
    Posts:
    133
    Does anyone knows if there is JS version of this script?
     
  38. Austin_CLS

    Austin_CLS

    Joined:
    Aug 25, 2013
    Posts:
    3
    Thanks a bunch for this script. It is pretty great.

    I am trying to combine a bunch of stuff in the editor and then lightmap it, but it seems that the 2nd UV set is not present on the combinedMesh. Ive done a little research on generating a new Secondary UV set using Unwrapping.GenerateSecondaryUVSet(mesh); but Im afraid getting it to work is a little above my head (Im a terrible programmer).

    Anyone have any ideas or want to take a crack at it?

    Thanks Again!
     
  39. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    You can save combined mesh as asset and reimport it with option "Generate Lightmap UVs"
     
  40. Austin_CLS

    Austin_CLS

    Joined:
    Aug 25, 2013
    Posts:
    3
    Hahah. This never even crossed my mind...

    Thanks man!
     
  41. Nidre

    Nidre

    Joined:
    Dec 12, 2012
    Posts:
    22
    @NeoDrop i have updated your script to be compatbile with WinRT.

    Here you can find the updated verison.I hope its ok for you that im posting here.

    http://snipt.org/Agjih1
     
  42. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    Sure, you welcome. I will repost it in first page.
    p.s. I did it ;)
     
    Last edited: Sep 17, 2013
  43. Kald

    Kald

    Joined:
    Apr 20, 2013
    Posts:
    16
    For some reason using this script renders all of my meshes invisible, and does not make any new game object with shared mesh. Any help with that?
    Ninja edit: Found out the reason for that. I tried to combine meshes that had more than 65000 vertices all together. Right now I need to think of a good way to displace them in groups with vertices count lower than that, but it would be awesome, if the script did that on the fly.
     
    Last edited: Sep 19, 2013
  44. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    UPDATE : New command ("Save mesh as Asset") added. Also, the script renamed to CombineChildrenExtended.cs
     
  45. dexForever

    dexForever

    Joined:
    Jun 26, 2013
    Posts:
    1
    Hello Neodrop.
    Sorry, but i am newbie in unity and i have some problems. In my game i have 9 cubes, which consist of 6 planes for each side. And i added your script for each cube. Its all okay when i play it in unity player. But when i test it in ipad, all cubes are disappear. Will be grateful for the help.
     
  46. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    Will try do not use "triangle strips" options. May be it can help.
    Also, I'm really hate iOs ;)
     
  47. galent

    galent

    Joined:
    Jan 7, 2008
    Posts:
    1,078
    Hey Neodrop,

    My generated mesh shows 0 Verts, so, I get a nice combined invisible mesh :) . Any idea what I'm doing wrong?

    Cheers,

    Galen

    [Never mind... I found it.. I should really read the error logs .. occasionally :D]
     
    Last edited: Nov 1, 2013
  48. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    @NeoDrop .. you are the f*** boss ..

    Thanks so much for this.. i was having some light map problems with the first release of script...
     
  49. zebra-tdi

    zebra-tdi

    Joined:
    Apr 18, 2014
    Posts:
    2
    thanks for the script, it works nice. however if I try to use "Save mesh as asset" I get
    MissingComponentException: There is no 'MeshFilter' attached to the "stolica3dmax03" game object, but a script is trying to access it.
    You probably need to add a MeshFilter to the game object "stolica3dmax03". Or your script needs to check if the component is attached before using it.
    CombineChildrenExtended.SaveMeshAsAsset () (at Assets/scripts/CombineChildrenExtended.cs:211)

    in this line:

    AssetDatabase.CreateAsset(GetComponent<MeshFilter>().sharedMesh, path);


    after I call "Combine Now" I get a child objects and not child mashes
    if (materialToMesh.Count == 1)
    goes to ELSE part and generates new objects, thus MeshFilter does not exist.

    I tried adding simple mashes to the object but this did not work, any idea if this can be solved?

    thanks
    almir
     
    Last edited: Sep 22, 2014
  50. Neodrop

    Neodrop

    Joined:
    Oct 24, 2008
    Posts:
    1,359
    I'm not sure now, but may be you can try use "Combine now" command before saving mesh.