Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity, Visual Effects Graph - how can I programatically play + stop an effect?

Tags:

c#

unity3d

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:

img1

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:

img2

Now this parameter is visible in the particle prefab:

img3

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?

like image 717
T1000 Avatar asked Dec 27 '18 11:12

T1000


Video Answer


1 Answers

  1. 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>();
    
  2. 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);
    
like image 159
Ruzihm Avatar answered Sep 18 '22 18:09

Ruzihm