Search Unity

draw calls for sprites?

Discussion in '2D' started by jimmyo, May 31, 2014.

  1. jimmyo

    jimmyo

    Joined:
    Mar 20, 2014
    Posts:
    20
    I have a sprite sheet imported into Unity, sliced using the built-in Sprite editor. I import the sprites into my code using:

    Code (csharp):
    1.  
    2. static Sprite[] sprites;
    3. sprites = Resources.LoadAll<Sprite>("spriteSheet14");
    4.  
    and then display 10 sprites using:

    Code (csharp):
    1.  
    2. for (int i=0; i<10; i++) {
    3.     dtgo1a = new GameObject ();
    4.     dtgo1a.AddComponent<SpriteRenderer>();
    5.     dtgo1a.GetComponent<SpriteRenderer>().sprite = sprites[i];
    6. }
    7.  
    When running, the above code uses 1 draw call.

    However, when I want to color my sprites (in this case greenish) I use:

    Code (csharp):
    1.  
    2. for (int i=0; i<10; i++) {
    3.     dtgo1a = new GameObject ();
    4.     dtgo1a.AddComponent<SpriteRenderer>();
    5.     Renderer gosr = dtgo1a.GetComponent<SpriteRenderer>();
    6.     Color gocf = gosr.material.color;
    7.     gocf.a = 1f;
    8.     gocf.r = 0.2f;
    9.     gocf.g = 1f;
    10.     gocf.b = 0.2f;
    11.     gosr.material.color = gocf;
    12.     dtgo1a.GetComponent<SpriteRenderer>().sprite = sprites[i];
    13. }
    14.  
    When running, the above code uses 10 draw calls.

    I thought that when sprites are all on the same spritesheet that they get aggregated into one single draw calls. Am I missing something? / Is there a way for all these sprites to be displayed with a single draw call even when I want to color them?

    ..Jim
     
    Last edited: May 31, 2014
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,203
    That code results in a single draw call here. By the way, AddComponent already returns the component being added, so if you're using that you don't need GetComponent afterwards; just AddComponent by itself is fine.

    --Eric
     
  3. jimmyo

    jimmyo

    Joined:
    Mar 20, 2014
    Posts:
    20
    Thanks for trying it Eric (and thanks for the tip re AddComponent returning the component). So I have reduced the code to the following (and I *still* get 10 draw calls):

    Code (csharp):
    1.  
    2. for (int i=0; i<10; i++) {
    3.   dtgo1a = new GameObject ();
    4.   dtgo1a.AddComponent<SpriteRenderer>().material.color = Color.green;
    5.   dtgo1a.GetComponent<SpriteRenderer>().sprite = sprites[i];
    6. }
    7.  
    (And if I remove the portion ".material..." after the "dtgo1a.AddComponent<SpriteRenderer>()", it requires only 1 draw call).

    Could the difference be that I am using Unity Free and you are using Unity Pro?

    Thanks for any tips you might be able to provide.

    ..Jim
     
  4. Cjreek

    Cjreek

    Joined:
    Apr 23, 2013
    Posts:
    33
    Try this:

    Code (csharp):
    1. dtgo1a.AddComponent<SpriteRenderer>().color = Color.green;
    Don't change the color of the material but the color of the renderer itself.
     
  5. jimmyo

    jimmyo

    Joined:
    Mar 20, 2014
    Posts:
    20
    Awesome ... thank you so much!

    Unity docs say:
    "Unity can automatically batch moving objects into the same draw call if they share the same material and fulfill other criteria." So my guess is that by changing the color of the material of each sprite, Unity couldn't batch them because they were treated as different materials for each sprite.
     
  6. Cjreek

    Cjreek

    Joined:
    Apr 23, 2013
    Posts:
    33
    Yes I think this is what's going on. I found out that the SpriteRenderer color actually is the vertex color of the sprite, the material is not touched by this.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,203
    Yeah, your code didn't have the color stuff in it when I replied. ;) Indeed you want to change the sprite color, not the material color, since changing the material color creates new instances of the material.

    --Eric
     
    TomasJ likes this.
  8. jimmyo

    jimmyo

    Joined:
    Mar 20, 2014
    Posts:
    20
    Thanks Eric - I realized my mistake in simplifying the code down to the key parts, cut out too much .. then updated the post to include the color stuff. Thanks for the quick reply!
     
unityunity