Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Tomer-Barkan · May 23, 2014 at 09:41 AM · 2dspriteresourcesdrawcalls

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?

Comment
Add comment · Show 11
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tarlius · May 23, 2014 at 09:58 AM 0
Share

How are you setting the textures/loading the sprites?

avatar image Tomer-Barkan · May 23, 2014 at 10:06 AM 0
Share

With spriteRenderer.sprite = newSprite

avatar image Tomer-Barkan · May 26, 2014 at 04:32 AM 0
Share

Submitted a bug to unity $$anonymous$$m. No response so far.

avatar image Matheuz Tomer-Barkan · Oct 16, 2015 at 07:20 PM 0
Share

Did you get and reply yet? Could you also link the bug report? Thanks,

avatar image Tomer-Barkan Matheuz · Oct 16, 2015 at 08:24 PM 0
Share

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.

avatar image Tarlius · Oct 17, 2015 at 06:39 AM 0
Share

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

avatar image Matheuz Tarlius · Oct 17, 2015 at 04:15 PM 0
Share

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

avatar image Tarlius Matheuz · Oct 18, 2015 at 03:30 AM 1
Share

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.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges