Dynamic Batching Material Properties

Hi

I have many small objects in my scene and they use the same material, so the dynamic batching was working perfectly.

After I added a script that changes material color to make them look a little bit different from each other, the dynamic batching stopped working. It is something like that:

public class ChangeColor : MonoBehaviour {    

public Color[] m_Colors;
    
    void Start () {
        renderer.material.color = m_Colors[Random.Range(0, m_Colors.Length-1)];
    }
}

Is there a way to change material colors individually, without breaking the dynamic batching?

Thanks
Emre

No. Dynamic batching works only for identical materials.

Does unity support setting vertex colors on a mesh and using a material that multiplies texture color with vertex color? (Thus saving batching?)

Yes, that is possible. Though you need to write the shader on your own, as the default shaders from Unity don’t take vertex colors into account.

As you said, vertex color diffuse shader and adding vertex colors data at runtime solved the problem for most my objects. But there is a drawback. Adding vertex colors dropped the vertex count limit for dynamic batching from 300 vertices (with vertex pos, normals and UVs) to 225 vertices (with vertex pos, normals, UVs and vertex colors).

My objects are created and destroyed at runtime, but their position remains the same. If getting object positions from shader is possible (and if it is also possible when dynamic batching is working), then maybe it can be used to change colors. What do you think?