Search Unity

Emitting Particles via script in the Editor

Discussion in 'Editor & General Support' started by lightfoot218, Dec 11, 2014.

  1. lightfoot218

    lightfoot218

    Joined:
    Nov 28, 2012
    Posts:
    2
    I have a particle system where i'm emitting particles at locations that I'm reading from a text file, like so:

    Code (CSharp):
    1.  
    2. string[] lines = textFile.Split('\n');
    3.      
    4. foreach (string line in lines) {
    5.              
    6.        float[] position = new float[3];
    7.        float.TryParse(line.Substring(xCharStart, 8), out position[0]);
    8.        float.TryParse(line.Substring(yCharStart, 8), out position[1]);
    9.        float.TryParse(line.Substring(zCharStart, 8), out position[2]);
    10.              
    11.        emitter.Emit(newVector3(position[0], position[1], position[2]), Vector3.zero, size, Mathf.Infinity, color);
    12.        Debug.Log("emitted a particle");
    13.  
    14. }
    I'd like to do this in the editor so I can preview the particles when I select this particle system in the scene. I know particle systems that emit without script will simulate in the editor scene view when selected.

    I have this working in an editor script, and my print statements are getting executed, however no particles are being emitted. I'm using the same code at runtime and the particles are emitted fine.

    So, my question is, can I not use ParticleSystem.Emit() in the Editor? Or is something else going on?

    Thank you!
     
  2. kurtdog

    kurtdog

    Joined:
    Mar 31, 2014
    Posts:
    14
    I'm interested in the same thing! I have some effects that emit particles at specific locations. I'd like to preview the effect using something very similar to a particle system editor preview mode.

    This is what I have so far:
    EditorScript:

    void EditorUpdate()
    {
    if(simulate)
    {
    Debug.Log(".");
    myParticleSystem.Emit(pos, Vector3.zero, particleSize, particleLifetime, color)
    }

    }

    When I hit an editor GUI button, it sets a the private "simulate" boolean to true, and the Debug.Log statement works. The particle system however doesn't emit anything.
     
  3. kurtdog

    kurtdog

    Joined:
    Mar 31, 2014
    Posts:
    14
    It seems like particleSystem.Emit(1) works from the editor, but not particleSystem.Emit(position,velocity,size,lifetime,Color)
     
  4. kurtdog

    kurtdog

    Joined:
    Mar 31, 2014
    Posts:
    14
    Got it working, a little hacky but if you just set the particle system parameters manually and then call particleSystem.Emit(1); it works.

    What I mean is, instead of calling

    Code (CSharp):
    1. myParticleSystem.Emit(pos, velocity, particleSize, particleLifetime, color),
    you have to do the following:

    Code (CSharp):
    1.  
    2.                 myParticleSystem.transform.position = pos;
    3.                 myParticleSystem.startSize = particleSize;
    4.                 myParticleSystem.startLifetime = particleLifetime;
    5.                 myParticleSystem.startColor = color;
    6.                 myParticleSystem.Emit(1);
    7.  
    It also seems that particles emitted this way from the editor won't update. Meaning any time based parameter on the particle system won't work. It also means that particles you emit won't every get destroyed.

    I'm getting around this by editing these parameters manually, and by removing the last particle in the particleSystem.particles array every time I add a new one once a certain threshold of particles has been emitted.

    Code (CSharp):
    1.  
    2.                 float threshold= 500; // or whatever you want
    3.  
    4.                 if (myParticleSystem.particleCount > threshold)
    5.                 {
    6.                     //create a new array to store particles
    7.                     ParticleSystem.Particle[] particles = new ParticleSystem.Particle[myParticleSystem.particleCount];
    8.                     //store the particles
    9.                     myParticleSystem.GetParticles(particles);
    10.                     //copy particles, except the first one into a new array
    11.                     ParticleSystem.Particle[] newParticles = new ParticleSystem.Particle[myParticleSystem.particleCount - 1];
    12.                     for (int i = 1; i < myParticleSystem.particleCount; i++)
    13.                     {
    14.                         newParticles[i - 1] = particles[I];
    15.                     }
    16.                     //set the edited list of particles as the new list of particles
    17.                     myParticleSystem.SetParticles(newParticles, newParticles.Length);
    18.                 }
    19.  
    This isn't perfect but works and will hopefully help someone out there! haha[/I]
     
  5. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    253
    Hello from future. Can we use ParticleSystem.Emit(EmitParams,1) now?