Hi, this is something I'm trying to get my head around.. in Quest in addition to shaders it has a built in material system and using that this is easy, but in Unity I'm not so sure.
So, lets say I have 10 objects, all with the same shader. I want to make it so when I click on one, it flashes and slowly fades (lets say, using emissive colour). In addition, I want to be able to click rapidly on different ones and all have their flash effect to occur separately based on when they were clicked
the problem, or I guess what I think might be a problem, is when I change the material emissive colour, won't all objects with that shader flash? at least, if I change emissive in the inspector that's what happens. Do I need to make the shader a prefab or something so that all objects have their own instance of the shader?
thanks!
Replies
That does raise one other question though- what happens if I *do* want all materials changed? Do I just iterate all the objects and do it one by one? or is there a global 'change all' method
thanks
edit:
another followup Q
if I set a unique material property in a script (eg, with the flashing example), and it auto-creates an instance of the material, am I in doing so increasing the draw-calls?
And yup, a new material instance would mean an additional draw call.
the thing with doing it in script, won't I have to deal with un-coloured items in the editor and not see the colours until runtime?
If you want to see the material instances in editor mode, you could also try using ExecuteInEditMode in the script that does the tinting. You'll get error messages about leaks, however (which can more or less be ignored in this case).
I think in 3.5 they added per renderer overrides for SetTexture, SetFloat, etc.. functions which will work on a per renderer basis instead of material. that way you can keep the reference to the material instance while specifying a single parameter override for the renderer.
http://unity3d.com/support/documentation/ScriptReference/Renderer.SetPropertyBlock.html?from=MeshRenderer
It not documented but I assume it allows you to assign MaterialPropertyBlock object to a renderer and it will use that when rendering the mesh like how it shows on the MaterialPropertyBlock page.