Search Unity

Question Spawn Particle Programmatically

Discussion in 'Visual Effect Graph' started by stv001, Mar 29, 2023.

  1. stv001

    stv001

    Joined:
    Dec 8, 2019
    Posts:
    12
    Is it possible to add a particle one at a time?
    For example, can I trigger one particle to spawn when an event happens, e.g. trigger it from a script?
    This would be similar with let's say array.push() in JS.
     
  2. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,307
    There is
    SendEvent()
    method, however standard event is processed once at the end of frame, so the last call is used when you use it multiple times per frame (per instance).
    If you need more than one, then there is direct link method or graphics buffer.
    https://forum.unity.com/threads/new-feature-direct-link.1137253/
     
  3. stv001

    stv001

    Joined:
    Dec 8, 2019
    Posts:
    12
    I connected the Event directly to Initialize and sent via script an event to OnPlay

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.VFX;
    3.  
    4. public class oneParticle : MonoBehaviour
    5. {
    6.     public VisualEffect visualEffect;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.         visualEffect = visualEffect.GetComponent<VisualEffect>();
    12.         InvokeRepeating("callFunction", 0, 1);
    13.     }
    14.  
    15.     void callFunction(){
    16.         var eventAttribute = visualEffect.CreateVFXEventAttribute();
    17.         visualEffect.SendEvent("OnPlay", eventAttribute);
    18.         Debug.Log("Particle Spawned");
    19.     }
    20. }