简体   繁体   中英

Unity - Particle system - Particle count

Does any know if Unity has a way to count how many particles have been emitted by a particle system? So I could check to see if there has been an emission, like:

public ParticleSystem mySystem;
private int currentParticleCount;
private int lastParticleCount;

void Start () {
    lastParticleCount = mySystem.getEmissionCount();
}

void Update () {
    currentParticleCount = mySystem.getEmissionCount();
    if(currentParticleCount>lastParticleCount) {
        DoStuff();
    }
    lastParticleCount = currentParticleCount;
}

You can use ParticleSystem.particleCount to return the current number of particles. If that's not giving you the proper amount of particles, use the ParticleSystem.GetParticles function since that returns just the current number of alive particles. Below is an example for both of them:

private ParticleSystem ps;

// Use this for initialization
void Start()
{
    ps = GetComponent<ParticleSystem>();
}

// Update is called once per frame
void Update()
{
    Debug.Log("Particles Count: " + ps.particleCount);
    Debug.Log("Particles Alive Count: " + GetAliveParticles());
}

int GetAliveParticles()
{
    ParticleSystem.Particle[] particles = new ParticleSystem.Particle[ps.particleCount];
    return ps.GetParticles(particles);
}

The exact feature that you are asking for is not build it, BUT:

You can know the current particles displayed by the system, so you can make a counter that acumulates the number, or if you know the "displaying time" you can do the maths.

Knowing the current particles: ParticleSystem.particleCount https://docs.unity3d.com/ScriptReference/ParticleSystem-particleCount.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM