Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Jun 18, 2016 at 01:31 PM by VinnieH01 for the following reason:

This was reported as a bug

avatar image
0
Question by VinnieH01 · Jun 17, 2016 at 07:59 PM · bug-perhapsbatchingsetpass

Creating gameobjects using script and assigning sprites to some of them breakes batching (both static and dynamic). Bug?

So I create some gameobjects using a script :

 public Sprite spr;
 
     void Start ()
     {
         for (int i = 0; i < 100; i++)
         {
             GameObject obj = new GameObject();
             SpriteRenderer rend = obj.AddComponent<SpriteRenderer>();
             rend.sprite = spr;
             obj.transform.position = new Vector2(i,i);
         }
     }

This does what you expect it to do (it batches). However if I only assing sprites to some of them :

 public Sprite spr;
 
     void Start ()
     {
         for (int i = 0; i < 100; i++)
         {
             GameObject obj = new GameObject();
             SpriteRenderer rend = obj.AddComponent<SpriteRenderer>();
 
             if (Random.Range(0,2) == 1)
             {
                 rend.sprite = spr;
             }
 
             obj.transform.position = new Vector2(i,i);
         }
     }

The batches and setpass calls increase alot (depending on the amount of gameobjects that dosen't get a sprite assing). I've even tried making the objects static :

 public Sprite spr;
 
     void Start ()
     {
         for (int i = 0; i < 100; i++)
         {
             GameObject obj = new GameObject();
             SpriteRenderer rend = obj.AddComponent<SpriteRenderer>();

obj.isStatic = true;

             if (Random.Range(0,2) == 1)
             {
                 rend.sprite = spr;
             }
 
             obj.transform.position = new Vector2(i,i);
         }
     }

And it turnes out that it even breaks static batching, all this leads me to belive that this is a bug. Does anybody have an explanation to why this is happening or should I report this as a bug?

Here's a picture to clearify : (Look at setPass calls and Batches) alt text

img.png (50.7 kB)
Comment
Add comment · Show 10
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 tanoshimi · Jun 17, 2016 at 09:20 PM 0
Share

What are you trying to achieve by adding a spriterenderer component to every instantiated object, but only assigning a sprite to some of them? What value is the spriterenderer component without a sprite?

avatar image VinnieH01 · Jun 17, 2016 at 09:50 PM 0
Share

@tanoshimi Every object is a tile and I want to create every tile at the beginning of the game and then add/change the sprites.

avatar image tanoshimi VinnieH01 · Jun 17, 2016 at 10:02 PM 0
Share

But if you've got objects that don't currently need rendering (because they're in a pool waiting to be re-used, say) then they should be disabled anyway. I can't understand the use-case why you'd want an active gameobject with an enabled spriterenderer component, but no sprite assigned.

avatar image VinnieH01 · Jun 18, 2016 at 08:14 AM 0
Share

@tanoshimi I came here to find an explanation to why this is happening so I know if I should report it as a bug to get it fixed. I diden't come here to find a workaround so I dont understand why its relevant to know why I need spriterenderers without sprites.

avatar image grimmdeveloper · Jun 18, 2016 at 09:25 AM 0
Share

This might help. http://docs.unity3d.com/$$anonymous$$anual/DrawCallBatching.html

At the very bottom. "Currently, only $$anonymous$$esh Renderers are batched."

avatar image VinnieH01 · Jun 18, 2016 at 10:13 AM 0
Share

@grimmdeveloper but the spriterenderer should batch as you can see by the image. At least when I use a sprite atlas

avatar image grimmdeveloper VinnieH01 · Jun 18, 2016 at 10:59 AM 0
Share

Well I can say I've tried your solution with and without sprite renderer, as well as with a sprite and without one, none of them batched for me at all. v5.3.5p2

Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by Jessespike · Jun 17, 2016 at 08:42 PM

I wouldn't jump the gun and blame Unity for bugs just yet, chances are, you're missing something. I presume that when you add a Renderer, it's also creating a new material. Try assigning a shared material to all of the Renderers.

 //pseudo
 public Material mySharedMaterial;
 ..
 renderer.sharedMaterial = mySharedMaterial;

Also if you want instantiated objects to be statically batched, then you need to combine them with the Static Batching Utility

Comment
Add comment · Show 2 · Share
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 VinnieH01 · Jun 17, 2016 at 08:57 PM 0
Share

I could try but it uses the shared sprites-default material. I dont think this is the problem because this only happens when some objects dosent have a sprite as I explained earlier.

avatar image VinnieH01 · Jun 17, 2016 at 09:00 PM 0
Share

Yes as I thought the material wasent the problem.

Follow this Question

Answers Answers and Comments

44 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 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

SetPass calls - important to keep this number low? 5 Answers

Why is my game doing 175 SetPass calls and 2319 batches? 2 Answers

Sprites: Too many setpass calls? 1 Answer

Batching objects with Instantiate for optimization 2 Answers

IndexOutOfRangeException: Array index is out of range. 1 Answer


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