TExcelTAD
Home › Unity with C# › Particles, VFX Graph & Shader Graph

Particles, VFX Graph & Shader Graph

Sparks, smoke, magic, dissolving enemies and glowing pickups make a game feel alive. Unity gives you two particle systems and a node-based shader editor:

Tool Runs on Good for
Particle System (Shuriken, built in) CPU most effects: hits, dust, smoke, muzzle flashes; collides with physics; works everywhere
VFX Graph (com.unity.visualeffectgraph) GPU (compute shaders) huge counts (100k+ particles), complex simulations; needs compute support, so check your target platforms
Shader Graph (com.unity.shadergraph, included with URP) GPU custom materials: dissolve, outlines, scrolling lava, toon shading

The Particle System

Add GameObject > Effects > Particle System. The component is organized in modules: Main (duration, looping, start lifetime/speed/size/colour, max particles, simulation space), Emission (rate over time, bursts), Shape (cone, sphere, mesh), Color/Size over Lifetime, Noise, Collision, Sub Emitters, Renderer (material, billboard or mesh).

csharp
using UnityEngine;

public class HitEffects : MonoBehaviour
{
    [SerializeField] ParticleSystem sparks;          // one reusable system in the scene, Looping off, Play On Awake off

    public void PlayHit(Vector3 point, Vector3 normal)
    {
        sparks.transform.SetPositionAndRotation(point, Quaternion.LookRotation(normal));
        var emitParams = new ParticleSystem.EmitParams { applyShapeToPosition = true };
        sparks.Emit(emitParams, 20);                 // emit 20 particles now, no new GameObject
    }

    public void SetIntensity(float t)
    {
        var emission = sparks.emission;              // modules are structs that write through to the system
        emission.rateOverTime = Mathf.Lerp(5f, 60f, t);
    }
}

Reusing one system with Emit is much cheaper than instantiating an effect prefab per hit. For one-shot effect prefabs, set Stop Action to Disable and pool them (see Object Pooling), rather than Destroy.

How an emitter works: a model

An emitter is a loop: each frame it accumulates rate × dt (spawning whole particles and keeping the fraction for next frame, like the fixed-step accumulator), ages every particle, and recycles dead ones within a fixed Max Particles budget. The model uses a seeded generator so the output is reproducible:

csharp · try it yourself
using System;

var rng = new Random(7);
const int MaxParticles = 8;
var age = new double[MaxParticles];
var life = new double[MaxParticles];
var alive = new bool[MaxParticles];
double rate = 25, spawnDebt = 0, dt = 0.1;      // 25 particles/s, 10 FPS for readability
int spawned = 0, dropped = 0;

for (int frame = 1; frame <= 8; frame++)
{
    // 1. age and kill
    for (int i = 0; i < MaxParticles; i++)
        if (alive[i] && (age[i] += dt) >= life[i]) alive[i] = false;

    // 2. emit: accumulate fractional particles
    spawnDebt += rate * dt;
    while (spawnDebt >= 1)
    {
        spawnDebt -= 1;
        int slot = Array.IndexOf(alive, false);
        if (slot < 0) { dropped++; continue; }    // budget full: Max Particles reached
        alive[slot] = true; age[slot] = 0;
        life[slot] = 0.3 + rng.Next(0, 4) * 0.1;  // start lifetime 0.3..0.6 s
        spawned++;
    }

    int count = 0; foreach (bool a in alive) if (a) count++;
    Console.WriteLine(
quot;frame {frame}: alive {count}/{MaxParticles} {new string('*', count),-8} spawned {spawned}, dropped {dropped}, debt {spawnDebt:F1}"); }
frame 1: alive 2/8 **       spawned 2, dropped 0, debt 0.5
frame 2: alive 5/8 *****    spawned 5, dropped 0, debt 0.0
frame 3: alive 7/8 *******  spawned 7, dropped 0, debt 0.5
frame 4: alive 8/8 ******** spawned 8, dropped 2, debt 0.0
frame 5: alive 8/8 ******** spawned 10, dropped 2, debt 0.5
frame 6: alive 8/8 ******** spawned 12, dropped 3, debt 0.0
frame 7: alive 8/8 ******** spawned 13, dropped 4, debt 0.5
frame 8: alive 8/8 ******** spawned 15, dropped 5, debt 0.0

The debt line shows the accumulator: 2.5 particles per frame means two, then three, then two. When the budget is full, new particles are simply not emitted. That is what Max Particles does, and why an effect can look "thinner" when it is spammed.

VFX Graph

VFX Graph effects are assets edited as graphs of Spawn, Initialize, Update and Output contexts. From code you talk to the Visual Effect component through exposed properties and events:

csharp
using UnityEngine;
using UnityEngine.VFX;

public class PortalFx : MonoBehaviour
{
    static readonly int Intensity = Shader.PropertyToID("Intensity");
    [SerializeField] VisualEffect vfx;

    public void Open()
    {
        vfx.SetFloat(Intensity, 2f);     // an exposed property in the graph's Blackboard
        vfx.SendEvent("OnOpen");         // a custom event that starts a Spawn context
    }
}

Shader Graph

Create a graph with Create > Shader Graph > URP > Lit Shader Graph, build it from nodes (Sample Texture 2D, Time, Noise, Fresnel, Lerp...), and expose properties in the Blackboard. A classic dissolve: compare a noise texture with an exposed _Cutoff property, feed it into Alpha with Alpha Clipping on, and add an emissive edge where the values are close. Drive it from code like any material:

csharp
static readonly int Cutoff = Shader.PropertyToID("_Cutoff");   // the property's Reference name in the Blackboard

IEnumerator Dissolve(Material m)
{
    for (float t = 0f; t < 1f; t += Time.deltaTime)
    {
        m.SetFloat(Cutoff, t);
        yield return null;
    }
}

Each property has a display name and a Reference name; code uses the Reference (_Cutoff). For the theory of shaders, see the Shaders & Graphics Programming course.

Test yourself

Which system should you pick for 200,000 GPU-simulated particles?

  1. Particle System (Shuriken)
  2. VFX Graph
  3. Line Renderer
  4. Trail Renderer

An emitter runs at 25 particles per second at 10 FPS. Why does it emit 2, then 3, then 2 particles per frame?

  1. Random rounding
  2. It accumulates the fractional 2.5 particles and emits whole ones
  3. The budget is full
  4. Bursts

Which name does code use to set a Shader Graph property?

  1. The display name
  2. The property's Reference name, such as _Cutoff
  3. The node name
  4. The graph asset name