Question by
blacktiger588 · Jun 12, 2018 at 03:22 PM ·
materialdrawcallsmaterialpropertyblock
The same material on several objects with different property (color)
How to use the same material on several objects, if only the color value changes. Tried: MaterialPropertyBlock - 1 shared material was created. As a result: 1 common material is used, the color of each object is different, but the number of draw calls is the same as the creation of material for each object (on the object by draw call), the material is in the shader [PreRendererData]. How can I reduce the draw call if I have 50 objects on the scene with material that differs only in color?
public GameObject prefab;
public GameObject[] cubes= new GameObject[3];
void Awake()
{
block = new MaterialPropertyBlock ();
}
void Start () {
for (int i = 0; i < 3; i++) {
cubes[i]=Instantiate (prefab, new Vector3 (i, 0, 0), Quaternion.identity);
if (i == 0) {
color = new Color (1, 1, 1);
} else if (i == 1) {
color = new Color (1, 0, 1);
} else {
color = new Color (0, 0, 1);
}
Renderer _renderer = cubes [i].GetComponent<Renderer> ();
//_renderer.GetPropertyBlock(block);
block.SetColor("_Color", color);
_renderer.SetPropertyBlock(block);
}
}
Comment