Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

The same material on several objects with different property

Discussion in 'General Graphics' started by Stelette, Jun 14, 2018.

  1. Stelette

    Stelette

    Joined:
    Nov 3, 2017
    Posts:
    21
    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?
    Code (CSharp):
    1. public GameObject prefab;
    2. public GameObject[] cubes= new GameObject[3];
    3. void Awake()
    4. {
    5.      block = new MaterialPropertyBlock ();
    6. }
    7. void Start () {
    8.      for (int i = 0; i < 3; i++) {
    9.          cubes[i]=Instantiate (prefab, new Vector3 (i, 0, 0), Quaternion.identity);
    10.          if (i == 0) {
    11.              color = new Color (1, 1, 1);
    12.          } else if (i == 1) {
    13.              color = new Color (1, 0, 1);
    14.          } else {
    15.              color = new Color (0, 0, 1);
    16.          }
    17.          Renderer _renderer = cubes [i].GetComponent<Renderer> ();
    18.          //_renderer.GetPropertyBlock(block);
    19.          block.SetColor("_Color", color);
    20.          _renderer.SetPropertyBlock(block);
    21.      }
    22. }
    Create several materials is not possible, because each object has a unique color.Thanks in advance
     
    trapazza likes this.
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,589
    trapazza and leimiles like this.
  3. Stelette

    Stelette

    Joined:
    Nov 3, 2017
    Posts:
    21