问题
I have a shader on a gameObject in Unity, Lets call it FooShader. In FooShader there is a public property called _fooVal
I want to update in real time.
I've created a blank C# script on the same gameObject.
My first problem is how do I reference this FooShader
shader? And then once I have a correct reference change its _fooVal
property?
I've tried using the following to get a reference to the Shader but it doesn't work.
gameObject.material._fooVal
回答1:
If I'm understanding your question correctly, you cannot access the variables in a shader directly, you have to use the methods on the material object. Try the following code to get your renderer component and set the value of (for instance) a float in its shader:
Renderer rend = GetComponent<Renderer>();
rend.material.SetFloat("_fooVal", 2.0f /* Your value here */);
Here is the Unity3D reference on materials - there are also SetInt
, SetColor
, SetVector
, and other methods depending on what type of variable you are trying to update.
来源:https://stackoverflow.com/questions/32000049/dynamically-change-property-of-shader