- Home /
Dynamic batching
I'm trying to incorporate dynamic batching into my application. As I understand it, Unity should be automatically batching objects with the same materials. I have a prefab in my Resources folder that is cloned multiple times in the scene and its own texture added to it (one of three). The mesh is a low-poly object, so I know I'm under the limit for batching objects. If I pause the game and look in the editor, the material of the all of the cloned objects become "rocket-01-default (Instance) (Material)". My question is what do I need to do to get these objects to batch? Do they not batch because the material has (Instance) in it after it clones? If so, how do I change this?
Thanks.
Answer by Eric5h5 · Jul 16, 2010 at 07:34 AM
Adding a texture to each object makes it have its own unique material, so it can't batch.
I have three different textures and only one material, so there are three combinations. There are up to 10 bullets on the screen at once, so there would have to be duplicates, but these do not batch even though they have the same texture and material. Why is this?
@kieblera5: you cannot have one material when you assign different textures to the objects. That automatically makes them unique materials.
One more follow-up question. I just remade my entire setup. There are now three separate prefabs. Each prefab has its own material and accompanying texture set inside the prefab. The material and texture are no longer being set or altered in the code; however, two objects of the same material/texture are not batching. Did I miss something that you said or am I doing something else wrong?
@kieblera5: the objects must have fewer than 300 vertices each.
Answer by nerophon · Sep 20, 2012 at 03:31 PM
How to Make Objects Dynamically Batch in Unity
they must share the same material
the material must not be instanced (check material name in debug inspector)
batching is applied only to meshes containing less than approx 900 vertex attributes in total (check mesh properties for some idea about this; for starters turn off normals & tangents to reduce it)
the textures must not include alpha
scale breaks batching
lightmaps break batching
multipass shaders break batching
layering other objects in between objects trying to batch can break batching
if you are instantiating the object in code, you may be instancing the material; try re-applying the material asset through .sharedMaterial
even within single-pass shaders, some can batch higher vertex attribute counts than others... in particular if you use a shader that colors vertices you’ll be adding attributes
is there any reference for these things you can point us to, or have you experienced these things for yourself? How do you find out if a specific object is dynamically batched or not?
and what does "layering other objects in between objects trying to batch can break batching" mean exactly? how can you layer an object in between objects?
these points were learned through experience; the Unity docs are far from comprehensive on the subject. As for layering, I mean it literally: if you have a mesh spatially overlapping mesh objects you are hoping to batch, that batch may fail; simply move the intruding object out of the way (in world space) and watch your dynamic batch count rise :)
Answer by LitobyteSoftworks_answers · Dec 02, 2011 at 12:34 AM
Even so, <300 vertices and one single material for all, they won't batch if scaled in the editor, Also it seems that Cloned objects with correct settings won't batch either. When you Instantiate a clone, it will be carried out as myobject(clone) and its material as thematerial(clone) this will result in not banching the object, and this is though to spot, as these objects are generated at runtime, so..
Try to put your batching materials into the resource folder, so that you can apply later the correct material, this should do the trick
Answer by djarcas · Nov 29, 2012 at 04:58 AM
There appears to be a huge list of 'what breaks batching'. Is there any way to definitely examine an object, and unity say "This object cannot be batched due to alpha textures" (or whatever) ?
It seems far too easy for your artist to accidentally break batching by making an object 301 polys, or including 1 pixel of alpha, and no-one will have the slightest clue why the framerate suddenly plummeted!
Answer by Madrayken · Mar 19, 2013 at 12:57 PM
Other things that break batching:
Turning on Mesh Compression! No idea why.
Using a shader that alters a vertex position / colour / UV, as this automatically creates an instance of the material.
Thank u, my models had mesh compression and i didn't know why it didn't batched, and i was very nervous cuz i triyed ething!
$$anonymous$$esh Compression: According to the changelogs, this was a regression introduced in 4.0 and fixed in 4.2
Your answer
Follow this Question
Related Questions
How to do iPhone Optimization with materials, textures and callbacks? 1 Answer
Batching and UVs - What are the limitations? 2 Answers
Optimizing bump map or luminous textures on spheres for mobile 1 Answer
Should I build my level with few meshes or many meshes on the iPhone? 1 Answer
Shader / Material Refreshing, AssetBundles, and dynamic batching issues 0 Answers