- Home /
This was reported as a bug
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)
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?
@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.
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.
@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.
This might help. http://docs.unity3d.com/$$anonymous$$anual/DrawCallBatching.html
At the very bottom. "Currently, only $$anonymous$$esh Renderers are batched."
@grimmdeveloper but the spriterenderer should batch as you can see by the image. At least when I use a sprite atlas
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
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
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.