In this lesson, you will learn how to expose material properties as variables in Shader Graph to enable real-time aesthetic adjustments through scripts or the Inspector.
Bridging the Gap Between Logic and Visuals
Up until now, we have used Shader Graph to define the static look of our surfaces. However, a static material is often insufficient for dynamic gameplay. Imagine a sci-fi door that needs to glow brighter when powered up, or a character’s skin that shifts color based on their health status. By promoting our graph nodes to Properties, we create "knobs" that we can twist while the game is running.
Exposing Properties
To turn a node value into an interactive variable, open your Shader Graph and locate the Blackboard window (usually on the top left). When you create a property—such as a Float for "Glow Intensity" or a Color for "Base Tint"—it becomes available globally within that shader.
When you assign this shader to a Material, these variables appear directly in the Inspector. You can then animate them via the Animation window or, more powerfully, manipulate them through C#.
Controlling Materials via Script
Once a property is exposed, you can use the Material class to communicate with the shader at runtime. Here is how you might trigger a "damage flash" effect by updating a property defined in your graph:
public class MaterialController : MonoBehaviour
{
private Material mat;
void Start() {
// Access the material instance assigned to the Renderer
mat = GetComponent<Renderer>().material;
}
public void SetGlow(float intensity) {
// Use the exact property reference name from your Shader Graph
mat.SetFloat("_GlowIntensity", intensity);
}
}
By using mat.SetFloat or mat.SetColor, you are essentially bypassing the static nature of the asset and injecting gameplay data directly into the GPU pipeline. This technique is the standard way to create reactive environments, shifting the visual identity of your objects based on game events rather than just pre-baked textures.
Key Takeaway: Exposing shader nodes as Blackboard properties allows you to bridge the gap between your game logic and visual output, enabling dynamic, script-driven material states.