- Home /
Dynamic setting of sprites cause many draw calls
I'm building a 2D game and dynamically loading the sprites from resources (so I can use a config file to specify which sprites belong to which item). I'm getting LOTS of draw calls as I instantiate more and more objects, even though they use the same 4 sprites (which I only load once with Resources.Load()).
Any ideas?
Edit: Code to reproduce -
void Start () {
Sprite[] sprites = new Sprite[] {
Resources.Load<Sprite>("Sprites/BuildingSprites/Farm"),
Resources.Load<Sprite>("Sprites/BuildingSprites/Lab")
};
for (int x = -10; x < 10; x++) {
for (int y = -10; y < 10; y++) {
GameObject obj = new GameObject();
obj.transform.position = new Vector3(x, y, 0);
obj.AddComponent<SpriteRenderer>().sprite = sprites[Random.Range(0, 2)];
}
}
}
Edit 2 - Same thing happens if I load the sprites from the inspector... So it seems like setting the sprite
property of a SpriteRenderer
causes it to create a new material, which will result in a separate draw call.
Any ideas on how to set the sprites dynamically without getting tons of draw calls?
Submitted a bug to unity $$anonymous$$m. No response so far.
Did you get and reply yet? Could you also link the bug report? Thanks,
Nope, never got a response. Even sent them a scene where this reproduces. Unity support are not known for following up. Not unless you're blizzard or something.
The only time I filed a bug I got a reply within a week and a fix in next version. (I am not Blizzard/etc)
Do the sprites overlap? The way you are using sprites you won't be using an atlas I think, so if you have a case where Farm is in front of Lab which is in front of Farm you'll generate an extra drawcall anyway I think (and an extra call for every occurrence of this).
Overlapping generates a new draw call indeed, doesn't matter if the sprites are in the same atlas. If you need to draw A on top of B and they are in the same atlas, 2 draw calls are needed: first one for B and another one for A (since it's on top).
As long as it the same atlas/sprite, with nothing in between you will get no extra draw calls:
https://gyazo.com/3bfce3e745778f4f843bcbffe8d916de
If you have a second set of image/atlased images all on top, you get 1 extra draw call:
https://gyazo.com/29be9b531519b0c76d7567a4aca8e795
If you have sprites in the middle, you get an extra draw call:
https://gyazo.com/112e387e3dbec85b76f16897503cd76e
I tried putting the images directly in the post but it didn't work - -;;
Edit:
This is using Unity's built in sprite renderer on unity 4.6.x. I doubt its changed in 5 though.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145712im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Struggling to change the sprite on the SpriteRenderer through code? 2 Answers
Problem with 2D Sprites and draw calls 1 Answer
How can I make a layer black? Unity 2D 1 Answer
Controlling a 3rd Person 2D Sprite in a 3D Environment 0 Answers
Trail renderer not rendering over sprite background 4 Answers