Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  3. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

[VFX Graph] Slow down or stop VFX / programmatically access Rate from Play Controls?

Discussion in 'Graphics Experimental Previews' started by skoteskote, Oct 22, 2019.

  1. skoteskote

    skoteskote

    Joined:
    Feb 15, 2017
    Posts:
    87
    Hi, how do I slow down/stop the particles of a VFX graph programmatically? Just like pulling the "Rate" slider in the VFX Play Controls in the scene editor down to 1. Can I somehow access the Rate of the Play Controls or is there another way? VFXRate.gif
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    @jakobskote just the same way as you can control other parameters, but you have to do it from code. I'll attach example in a moment.

    Code (CSharp):
    1.  
    2. // Remember to include this
    3. using UnityEngine.Experimental.VFX;
    4.  
    5. public class AdjustParticleParams : MonoBehaviour
    6. {
    7.     // Your visual effect gets assigned to this in UI (or do it in code)
    8.     public VisualEffect vfx;
    9.  
    10.     [Range(0,1)] public float playRate;
    11.  
    12.     void Start()
    13.     {
    14.         vfx = GetComponent<VisualEffect>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         vfx.playRate = playRate;
    20.     }
    21. }
    22.  
    If you want to control your own, arbitrary parameters you might have exposed in the Blackboard of the VFX Graph, you can do it this way:

    Code (CSharp):
    1. public VisualEffect vfx;
    2. public string exposedParameterName = "Spawn Rate";
    3. [Range(0,1000)] public int spawnRate;
    4.  
    5. void Start()
    6. {
    7.    vfx = GetComponent<VisualEffect>();
    8. }
    9.  
    10. void Update()
    11. {
    12.    vfx.SetUInt(exposedParameterName, (uint) spawnRate);
    13. }

    Expose it like this:
    blackboard.PNG

    It should look like this in Inspector:
    Inspector.png

    Then connect it to the Spawn node (well, it could be anything...)
    spawn.PNG
     
    Last edited: Oct 22, 2019
  3. skoteskote

    skoteskote

    Joined:
    Feb 15, 2017
    Posts:
    87
    @Olmi Awesome! Thank you so much for such an extensive reply. :)
     
  4. Angelo13C

    Angelo13C

    Joined:
    Feb 16, 2020
    Posts:
    11
    It doesn't work for me and I have the same setup as @Olmi (I can even see in the inspector the property value that changes, but the particles spawn at the same rate)