draw calls for sprites?

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

static Sprite[] sprites;
sprites = Resources.LoadAll<Sprite>("spriteSheet14");

and then display 10 sprites using:

for (int i=0; i<10; i++) {
	dtgo1a = new GameObject ();
	dtgo1a.AddComponent<SpriteRenderer>();
	dtgo1a.GetComponent<SpriteRenderer>().sprite = sprites[i];
}

When running, the above code uses 1 draw call.

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

for (int i=0; i<10; i++) {
	dtgo1a = new GameObject ();
	dtgo1a.AddComponent<SpriteRenderer>();
	Renderer gosr = dtgo1a.GetComponent<SpriteRenderer>();
	Color gocf = gosr.material.color;
	gocf.a = 1f;
	gocf.r = 0.2f;
	gocf.g = 1f;
	gocf.b = 0.2f;
	gosr.material.color = gocf;
	dtgo1a.GetComponent<SpriteRenderer>().sprite = sprites[i];
}

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

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

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):

for (int i=0; i<10; i++) {
  dtgo1a = new GameObject ();
  dtgo1a.AddComponent<SpriteRenderer>().material.color = Color.green;
  dtgo1a.GetComponent<SpriteRenderer>().sprite = sprites[i];
}

(And if I remove the portion “.material…” after the “dtgo1a.AddComponent()”, 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

Try this:

dtgo1a.AddComponent<SpriteRenderer>().color = Color.green;

Don’t change the color of the material but the color of the renderer itself.

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.

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.

Yeah, your code didn’t have the color stuff in it when I replied. :wink: Indeed you want to change the sprite color, not the material color, since changing the material color creates new instances of the material.

–Eric

1 Like

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!