- Home /
Changing objects texture causes high draw call count!
Part of the game I am working on is avoiding bombs. If you hit a bomb it will "burn" everything around it. I am doing the burn by changing the texture atlas out for a burnt style atlas. The problem is that each time the texture swap happens, it seems to add 3-15 draw calls pending on how many objects are being hit. The bomb instantiates graphic for the explosion. This graphic is what triggers the texture switch. Here is the code on the graphic.
#pragma strict
var object : GameObject[]; object = GameObject.FindGameObjectsWithTag("pig"); var texture : Texture; var test = false;
function Awake () { //destroy after .1 seconds Destroy(gameObject, .1); test = true;
} //look at the camera function Update(){
transform.LookAt(transform.position + Camera.main.transform.rotation Vector3.down,Camera.main.transform.rotation Vector3.back);
if(test == true){ test = false; for (var obj: GameObject in object){ obj.renderer.material.mainTexture = texture; } }
}
With near max objects on screen I am only at 2 draw calls. When I detonate a bomb it jumps to 15 instantly. The draw calls go down as the items destroy themselves after being hit by the bomb.
I cannot figure out why swapping the textures would cause such a problem. Would it be a better idea to have the explosion graphic change a variable on the objects hit by the explosion, then that variable controls the texture change? Then each object hit changes its own texture, instead of one object changing +10 textures.
Answer by whydoidoit · Jul 15, 2012 at 03:10 PM
Your problem is that as soon as your set the texture you are getting a copy of the material for each of the objects - none of them batch after that as they all have different materials.
I suggest you have a burned material which you set on the objects rather than changing the texture, this material would already have it. Then maybe set the sharedMaterial (though I can't quite remember whether you need to do that).