Reputation: 301
I have 81 game objects in my scene. When I play the stats are healthy with 3 batches running. But when I access the renderer in the script the batches count increases for every object. After I accessed all the objects' renderer the situation gets bad.
Before: Batches: 3 | Saved by batching: 80 | FPS: Nearly 400
After: Batches: 83 | Saved by batching: 0 | FPS: about 200
Code: transform.GetComponent<Renderer>().material.color = Color32.Lerp(start_color, target_color,t);
I think the question makes sense. "Why did Unity3D did not cost an object a batch before its renderer is accessed? Why is that batch count still active even after I did my job with the game object?"
Updates: 1. Even if I simply access the renderer without changing it, it costs a batch and never it gets decremented.
start_color = transform.GetComponent<Renderer>().material.color;
I scale the objects by altering the color through lerp. In the inspector, the material gets renamed as mat(instance).
I tried using sharedMaterial. Now the batches count is stable as 3 but all the 81 objects change to the same color. Story: The 81 can be updated to low, medium, high states with different grey shades. I need to group them categorized when updated.
Upvotes: 0
Views: 1208
Reputation: 3019
@VladislavKurenkov is right. There also is another trick, but it won't work in all scenarios - instead of referencing the material via GetComponent().material just make a public variable of type Material. Then modify whatever you want on that variable and all the transforms will get affected. Just feed a material from your Assets into the inspector slot. This will save batch.
Upvotes: 1
Reputation: 88
Every time you change any properties of a material it will create an instance of the material. This is why your objects aren't saved by batching.
Upvotes: 1