unity-game-engineshader-graph

Access alpha attribute of shader graph through script in Unity


I wish to control the transparency of a model during runtime.

The model's material is connected to a shader graph. After changing the Surface Type of the shader graph to Transparent, an Alpha attribute became available to control the transparency of the model (shown in the image at the bottom).

enter image description here

However, I do not know how to access this Alpha attribute in the script.

I have tried OBJ.GetComponent<Renderer>().material.SetColor() and it did not work, since Material 'MATERIAL_NAME' with Shader 'SHADER_NAME' doesn't have a color property '_Color'.

Is there a way I can access this Alpha attribute outside of the shader graph in a script?


Solution

  • In order to create publicly available variables in your shader graph, you are going to want to open the Blackboard window and create a new Float type variable. Use the Graph Inspector window to mark your variable as exposed and give it a custom reference if desired. Once exposed, this variable can be changed from the material window or from a C# script using the reference name.

    You can drag this variable out of the Blackboard window and connect it to the alpha in order to control that property.

    Unity graph inspector