Search Unity

Dynamic Batching Failing for RTS Game

Discussion in 'Editor & General Support' started by Weev, Apr 27, 2012.

  1. Weev


    Oct 10, 2009
    I posted a question on Unity Answers, but there haven't been any answers, so I thought I'd post here as well in case it hits a wider audience.

    Question on UA:

    To summarize what's there:
    I've got a simple RTS game where the "terrain" is a bunch of grid cells, where each cell is a 4-vertex plane. So a 30x30 grid uses 900 planes. When I'm drawing only the terrain, it all gets batched into a single draw call, exactly as expected.

    The structures in the game are all low-vertex models (the most complex being 117 vertices), and all use the same material as the terrain planes. When I draw only the structures, they all get batched into a single draw call. Again, exactly as expected.

    However, when I'm drawing both the structures and the "terrain", I get over 400 draw calls. Everything I can find related to dynamic batching doesn't work, so I'm not sure what's causing the problem.
    - Everything is scaled (1,1,1)
    - The materials on the objects aren't instances
    - The material uses a single-pass, unlit shader with no transparency. It uses vertex colors to tint a texture.
    - There are no lights in the scene.
    - Using the Forward or Vertex Lit rendering path doesn't make a difference. (I'm using the free version of Unity)
    - All the Mesh Filters and Mesh Renderers are in the same layer (does this even matter? Did I misread something to make me think it might?)

    My other question also describes some weirdness I'm seeing where the relative position of the objects to each other affects the number of draw calls, and I'm not sure why that makes a difference or how to fix it.

    I'd really appreciate any help anyone could provide.
  2. _Petroz


    May 13, 2010
    Have you tried parenting the objects separately? So create one parent for all the terrain objects, and one parent for all the building objects. My understand is dynamic batching is dependent on both materials and parenting.
  3. Glockenbeat


    Apr 24, 2012
    Confirmed. Bad parenting breaks dynamic batching. Also, do you use Skinned Mesh Renderers? Those don't batch at all.
  4. Weev


    Oct 10, 2009
    I'm using Path-o-logical's PoolManager to pool my objects, and the terrain objects and structures are in separate pools, but are both under a root "Pools" object:

    --(Terrain Objects)

    However, there are some other pools under the root "Pools" object that may not be able to batch with the terrain or structures. Is that the problem? In fact, when I say I'm "drawing only the terrain", it's by disabling the other pool root objects (e.g. "Structures"), so only one pools is being drawn, so I guess that makes sense.

    So, would the solution be to divide up my pools by what can be batched? For example:

    Pool Batch 1
    Pool Batch 2
    -Other Stuff

    @Glockenbeat: No skinned mesh renderers, just the standard "MeshRenderer".
  5. Weev


    Oct 10, 2009
    Well, changing the parents fixed it. Now I'm getting just a handful of draw calls, as I expected. This was the one thing I was missing... Thanks so much for the quick answers.

    If either of you wants credit on Unity Answers, just post an answer there, and I'll give you the thumbs-up and answer credits you deserve. :)
  6. Rafes


    Jun 2, 2011
    I wanted to post this here as well just in case anyone else has confirmed this as an issue...

    I was excited to try this because we have experienced some batching issues while developing a Tower Defence game (similar to RTS in assets). Doubly so because I wrote most of PoolManager. I commented out the code that parents instances under the pool gameObjects and it didn't make any difference. We seem to be running between 30 and 60 draw calls either way.

    Regardless, if it helped you, it could help others. Please email and I can at least expose the option in the inspector.

    PoolManager will work fine with or without parenting. It is just for scene organization.

    In my test scene we have:

    Pools (This is actually a prefab full of GameObjects, each with a SpawnPool)
    - Enemies
    -- Many different enemy instances that share a material with buildings
    - Buildings
    -- Many different buildings that share a material with enemies
    - Effects
    -- Many different effects

    We have more, but you get the idea. By commenting out the parent lines of code, all instances went directly under the scene root. I saw the same number of draw calls.

    I'll have to try some more extreme divide-and-conquer once the game is further along. E.g. turning effects off to see if the particles are breaking batching, etc..
  7. Ross_S


    Jul 10, 2012
    Hi folks,

    [EDIT: Oh HANG ON.. that was my fault... i was scaling in code after spawning... Duh! excuse me PoolManager.]

    Just thought i'd flag that i'm running into what seems to be a similar problem here. I have a very simple scene object that if I duplicate it in the scene by hand in the editor will not increase my draw calls...
    However the clones created by PoolManager do increase my draw calls. I've tried re-parenting the clones so they are all under an empty GameObject in the scene - where the original prefab that i'm cloning from also is... but this isn't working.
    Weez - when you say you changed the parent and it fixed it can you specify a little more exactly what you did? I guess there's not much to it? but doesn't seem to be working for me. Thanks.