Dynamically change property of Shader

戏子无情 提交于 2021-01-28 02:50:34

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!