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

Excess draw calls

Discussion in 'Scripting' started by delinx32, Nov 20, 2014.

  1. delinx32

    delinx32

    Joined:
    Apr 20, 2012
    Posts:
    417
    I have a scene in which I was testing an dynamic mesh script in the editor. I must not have been destroying things correctly because I get about 400 draw calls when I run my game. I created a duplicate of the scene and I get 16 draw calls.

    Is there some way to clear a scene's memory/objects? I deleted everything in the scene and still get 400 draw calls. I could just create a new copy of the scene because there isn't much in it, but I'd like to know how to fix this if it happens again in the future on a more developed scene.
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    How are you destroying the objects?
     
  3. delinx32

    delinx32

    Joined:
    Apr 20, 2012
    Posts:
    417
    I am destroying them using DestroyImmediate. I don't think its a current problem. My objects appear to be getting destroyed properly now. The issue is ones that I may not have destroyed properly in the past. There are close to 400 "things" that are using draw calls, even when I delete everything from my scene. I need to know how to clean those up.

    I made a new scene to fix the problem, but for future reference i'd like to know how to clean up leaked objects (if there is a way).
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Last edited: Nov 21, 2014
  5. RSG

    RSG

    Joined:
    Feb 20, 2013
    Posts:
    93
    It's hard to tell what could have caused the draw calls based on what you described. Since you are creating dynamic meshes, check your code to make sure that you are batching your meshes properly. Draw calls are based on the content that you display in the scene; you could have 400 objects each with a unique material, or just one with 400 different materials resulting in 400 draw calls.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Draw calls roughly equate to separate materials. A wide range of activities can cause "extra materials" to be in your scene, despite your originally only referencing a single material. For instance, some of these things can cause otherwise-shared materials to be instanced.

    - setting the .color field in a material
    - setting the texture field(s) in a material
    - setting the texture offsets, tilling in a material
    - scaling the game object's transform (this is discussed elsewhere extensively)

    If you have a single mesh you are fabricating and (for instance) 16 different materials, then instantiate those materials and keep them in your own array, and then "point" each mesh at those originals, and that should give Unity the best chance to share them, and thus reduce your drawcalls.

    Remember, there is also an upper limit to how much geometry can go out with a single drawcall, and eventually you will need additional draw calls even if you have a single material.

    And yeah, don't use destroy immediate. Just destroy it; that's a common source of weirdness. Destroy immediate is really only for editor scripts, as far as I understand it.
     
  7. delinx32

    delinx32

    Joined:
    Apr 20, 2012
    Posts:
    417
    So, if I delete every object in my scene (completely empty hierarchy), and still get 400 draw calls, what would be causing them?
     
  8. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Multiple lights and shadows generate tonnes of extra draw calls.