I want to use the Visual Effect Graph
to make a particle explosion. I am trying to activate and stop this explosion programmatically.
I first create a Particles
prefab object, and attach a Visual Effect
Graph object to it:
Then I make the spawn rate
an exposed parameter
. So when the explosion activates it will set this parameter to 100. To stop the explosion, the spawn rate will be set to 0:
Now this parameter is visible in the particle prefab:
Then I instantiate the Particles prefab:
var effect = Instantiate(particlesPrefab, position), Quaternion.identity);
The particle effect shows up on the screen, but the problem is I can't find the spawn rate
attribute.
How can I programmatically change this attribute's value?
Get the VisualEffect
from the Particles
GameObject
:
// In a MonoBehaviour attached to the Particles GameObject
using UnityEngine.Experimental.VFX;
...
// As a field in the MonoBehaviour
public VisualEffect myEffect;
...
myEffect = GetComponent<VisualEffect>();
Use SetInt
to set the exposed integer called "spawn rate"
:
// As class field
public static readonly string SPAWN_RATE_NAME = "spawn rate";
// Wherever you want to stop explosion
myEffect.SetInt(SPAWN_RATE_NAME, 0);
// Wherever you want to start explosion
myEffect.SetInt(SPAWN_RATE_NAME, 100);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With