Search Unity

Change vertex colors for individual objects without using multiple mateirals

Discussion in 'Shaders' started by will_brett, Jul 18, 2015.

  1. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Hi there,

    I am really not great with shaders. I played around with them a year ago and made some cool stuff but don't really understand what im doing.

    However I wrote a shader that allowed me to change the colors of individual gameobjects without having to create new materials each time. Im pretty sure it still allowed for batching too. I have since lost the shader and could really do with it again.

    Anyone have any ideas where to start looking? I really can't remember how I did it.
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    You actually don't need a special shader to do this. When you make or edit a material in unity, it is a shared material, so any changes you make will affect any object with the material. However, your mesh renderer has an instance of that shared material, and any changes you make to that instance will only apply to the one object. To access this instance, you just call MeshRenderer.material, then on that material you can call material.SetFloat, material.SetColor, material.SetTexture, etc.
     
    will_brett likes this.
  3. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Thanks. How would you see these changes in editor. I can see how this would work in play mode but I want to be able to do this a lot whilst building my environments.
     
  4. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    I don't think you're meant to be able to do this and you're supposed to just make more materials...

    However, a custom inspector with code that runs at edit time may allow you to do this. Just have the editor code edit the material the same way you did at runtime.

    Here's a handy tutorial on how to make a custom inspector button that runs code in the editor: https://unity3d.com/learn/tutorials/modules/intermediate/editor/adding-buttons-to-inspector