How can I change the color of an object's material, not generate another instance of the material, and still keep batching?


I'm trying to optimize my drawcalls on an iPhone project, and it's my understanding that changing the material color generates a new material instance and hence another drawcall.

I read in the forums that using a VertexLit shader and modifying vertex colors will let you change the color of an object and still keep it dynamically batched.

I don't know much about shaders, but in my code I am modifying the "material.color" property, thinking thats how you change the Vertex colors. It's still creating a new material though, so I must be doing something wrong.

So given two objects that share the same material, object A and object B. How can I make: A = red B = blue and still keep batching?

here is a code snippet of how i'm currently doing it

function Start()
    thisMaterial = renderer.material;
    origColor = thisMaterial.color;
    fadeToColor = origColor;

    if (unitType > 0)
       fadeToColor.b = 1;
    else if (unitType < 0)
       fadeToColor.r = 1;

function Update()
    if (unitType != 0)
       thisMaterial.color = Color.Lerp(origColor, fadeToColor, Mathf.PingPong(Time.time, 1));

You have to change vertex colors by changing the actual colors of the vertex color array from a mesh. Changing material.color only changes the color of the material.

You could render them with individual colors in one batch using Graphics.DrawMesh and MaterialPropertyBlock: