0
0
Unityframework~10 mins

Shader Graph basics in Unity

Choose your learning style9 modes available
Introduction

Shader Graph lets you create cool visual effects for games without writing code. You connect blocks to design how things look.

You want to make a shiny or glowing surface in your game.
You need to create water or glass effects easily.
You want to change colors or textures based on game actions.
You want to learn how graphics work in a simple way.
You want to make your game look unique with custom visuals.
Syntax
Unity
1. Open Unity and create a new Shader Graph asset.
2. Double-click the Shader Graph to open the editor.
3. Add nodes by right-clicking and selecting from the menu.
4. Connect nodes by dragging from output ports to input ports.
5. Save the graph and assign it to a material.
6. Use the material on your 3D object in the scene.

Shader Graph uses a visual node system instead of text code.

Each node does a small job like color, math, or texture.

Examples
This makes the whole object red using a simple color node.
Unity
1. Create a Color node and connect it to the Base Color input of the PBR Master node.
2. Change the Color node to red to make the object red.
This example shows how to animate glow using time and math nodes.
Unity
1. Add a Time node and connect it to a Sine node.
2. Connect the Sine output to the Emission input of the PBR Master node.
3. This makes the object glow rhythmically over time.
This example applies an image texture to the object.
Unity
1. Use a Texture2D node to add an image.
2. Connect the Texture2D output to the Base Color input.
3. The object will show the texture image on its surface.
Sample Program

This script shows how to assign a Shader Graph material to a 3D object in Unity. It prints the material name before and after applying the Shader Graph material.

Unity
// This is a Unity C# script to create and assign a Shader Graph material
using UnityEngine;

public class ApplyShaderGraphMaterial : MonoBehaviour
{
    public Material shaderGraphMaterial;

    void Start()
    {
        if (shaderGraphMaterial != null)
        {
            Renderer objectRenderer = GetComponent<Renderer>();
            if (objectRenderer != null)
            {
                Debug.Log("Before applying Shader Graph material: " + objectRenderer.sharedMaterial.name);
                objectRenderer.material = shaderGraphMaterial;
                Debug.Log("After applying Shader Graph material: " + objectRenderer.material.name);
            }
            else
            {
                Debug.Log("No Renderer found on this GameObject.");
            }
        }
        else
        {
            Debug.Log("Please assign a Shader Graph material in the inspector.");
        }
    }
}
OutputSuccess
Important Notes

Shader Graph runs on the GPU, so it is very fast for graphics.

Creating complex effects may need many nodes connected carefully.

Common mistake: forgetting to save the Shader Graph before using it in the scene.

Use Shader Graph when you want visual control without coding shaders manually.

Summary

Shader Graph lets you build shaders visually by connecting nodes.

You can create colors, textures, animations, and effects easily.

Assign Shader Graph materials to objects to see your effects in the game.