Search Unity

Render meshes with different colors but with the same material

Discussion in 'General Graphics' started by Bloody-Swamp, Dec 10, 2015.

  1. Bloody-Swamp

    Bloody-Swamp

    Joined:
    Jul 30, 2012
    Posts:
    38
    Hello folks,

    I'm puzzled on how to render multiple meshes with different colors each one using the same material because of the batching I want to have. Like SpriteRenderer where you can set different Color each time but you always have the same sprite and material so it won't break the batching. You have this "feature" also on LineRenderer with Start Color / End Color.

    Anybody has any idea how to achieve this?
     
  2. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102
    If you want to change the color, you should change it in this way:
    Code (CSharp):
    1. this.GetComponent<Renderer>().material.color=color;
    But, as you can read here, this workaround breaks the shared material reference:
     
  3. Zomby138

    Zomby138

    Joined:
    Nov 3, 2009
    Posts:
    659
    How about changing the vertex colours of the meshes and using those in the shader.
     
    karl_jones and Jordi-Bonastre like this.
  4. Bloody-Swamp

    Bloody-Swamp

    Joined:
    Jul 30, 2012
    Posts:
    38
    Thank you for your replies, I found my solution: mesh.colors32 = new Color32[] { ... };
     
    Jordi-Bonastre likes this.