How to set a parameter on a Shader Graph backed material at runtime without changing the actual material file?
I'm currently calling material.setVector("_MyVectorName")
on a Shader Graph backed material in one of my scripts. It works great, but the problem is that it changes the parameter in the actual material file too, which means the changes show up in my version control system every time I play the game.
Is there a way to set a parameter like this at runtime without actually changing a file?
Answer by jake_card · May 14, 2020 at 09:56 PM
I figured it out!
A .material
file is basically a material prefab, so when you change a parameter on it, you're changing the prefab. However, once you drag a material into a renderer component, you've now created an instance of that material, and you can make changes to that instance without affecting the original material.
So, if you're going to call material.SetVector
(or .SetWhatever
), make sure you call it on a material that you pulled out of a renderer (like by doing someRenderer.material.SetVector
) rather than directly on a material file.
Your answer
Follow this Question
Related Questions
Non-legacy alternative to Legacy Shaders/Particles/Additive 0 Answers
Voronoi node pattern looks differ between devices 0 Answers
Problem with Z testing??? 1 Answer
Is there any way to create and load a shader source file at runtime? 1 Answer
Is Zwrite On or Depth Only Clear Faster on mobile devices? 0 Answers