Search Unity

Reducing Draw Calls, its the Shared Materials???

Discussion in 'Scripting' started by gregmax17, Nov 21, 2013.

  1. gregmax17

    gregmax17

    Joined:
    Jan 23, 2011
    Posts:
    186
    Hello, I am struggling to reduce draw calls in my game. I am following ALL the rules of dynamic batching (no scaling, one texture, material, no where in code am I referring to "renderer", etc).

    But for some reason, my draw count is still way over 100. However, with this snippet of code:

    Code (csharp):
    1.  
    2. public Material mat;
    3. void Start() {
    4.   gameObject.renderer.sharedMaterial = mat;
    5. }
    6.  
    I was able to reduce reduces draw calls. The "mat" variable is the same material, so I am basically setting it self, again?

    I don't understand this... am I doing something wrong here? Please help me understand what is going on.

    Thanks you guys
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    If anywhere you are using gameObject.renderer.material (not shared) to change things like color or mainTexture, this changes the material, so you essentially create a a new one, then this object wont batch with other, similar objects.

    if putting gameObject.renderer.sharedMaterial = mat in a script that applied to all of your objects, all those objects will be using the same material, and can then be batched.
     
  3. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Have you tried the texture packer script? It will reduce your drawcalls, but increase your tris Verts.
    You can get it from here :
    http://forum.unity3d.com/attachment.php?attachmentid=20467&d=1305049108

    To use it,

    1. set all the used textures to advanced + read write
    2. set all the objects that you want batched to static
    3. create an empty gameObject and call it root
    4. Drag the static objects to root
    5. Add the script to it, let it compile then clear errors remove the script.
     
  4. gregmax17

    gregmax17

    Joined:
    Jan 23, 2011
    Posts:
    186
    Thanks for your answers and suggestions guys, though I didn't explain my problem correctly. Apologies... let me try again.

    I am creating ~20 objects (by instantiating) at start up. When I create these objects, it is creating a new instance of the material... for some strange reason, and this is what was pulling my hair. It wasn't referencing the original material (which would help because thats how I could reduce my draw calls). I was not doing any kind of scaling, changing material through code, material/shader is not transparent, etc. It would just create a new instance of my material. My script (as shown above), fixed this. But it was silly having to write it. Oh well.

    But I must do what I must. I will just settle with this, thanks for your help again fellas.
     
  5. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Woah! Only 20 objects and your drawcalls are way over 100?! They must be high poly. If they were below 300verts your drawcalls would be as low as 20.

    I have 100+ objects in a scene of my game and the drawcalls don't go above 70. Is the game your making for mobiles or PC? if PC then don't worry about drawcalls too much man.