- Home /
Recycling particle system
I am implementing an object pool using this implementation. It's already working fine with prefabs
but I don't know how to handle the recycling of particles system.
In my game when the main character picks stars I create a particle system by doing:
GameObject explosion = Instantiate (explosionPrefab) as GameObject;
explosion.transform.position = this.transform.position;
and after doing the emission it gets deleted because the Autodestruct
flag is on.
I was looking for a method or someway to know when the particle emission finishes to call:
ObjectPoolManager.DestroyPooled( explosion );
Any ideas?
Your question isnt very clear. $$anonymous$$ind elaborating what exactly you want it to do, in lamens...? =)
I want to know where I should destroy the particle system. It should be done after the particle finishes emitting.
Answer by GC1983 · May 03, 2012 at 07:21 PM
There are three ways you could go about this depending on how you are using the object the particles are attached to:
If you are keep the object after particles are executed, then just check the "One Shot" box in the particle options. It will set off the particles at one time, like an explosion, and not repeat it.
If you are destroying the object after use, then it should destroy the particles with it, which automatically solves your issue. If youre not, label the particle object attached to the main object and send a Destroy() to that object.
Or you could take this code and attach it to the particle object and sync the amount of time the particle emits to the timer that will destroy the object:
var timeOut : int = 1.0; var detachChildren : boolean = false;
//Set timer to destroy function Awake () { Invoke ("DestroyNow", timeOut); }
//Destroy Object attached function DestroyNow () { if (detachChildren) { transform.DetachChildren (); } DestroyObject(gameObject);
This will automatically delete the particle object when you please.
Hope this helps!
@Greg: 1) Careful. "One-shot" doesn't mean the particles are emitted only one time, it means the particles are emitted all at once ins$$anonymous$$d of over time. http://answers.unity3d.com/questions/46827/particle-emitter-to-emit-one-shot-on-specific-occa.html
2) The particle system might last longer than the object. For example the coin disappears before the explosion finishes.
3) This sounds like a good work around. I am not completely sure how to get the duration of the emission. How can I do that?
Ahh good point. Ill edit my answer. It still may solve his issue depending on the way the particles work. Ive used it for bomb explosions. $$anonymous$$y bad!
The duration is the $$anonymous$$ and maxEnergy. If you make them very small, it will make the emission very quick and short.
Your answer
Follow this Question
Related Questions
Recycling particle emitter leaves particles in 2 locations instead of 1 3 Answers
Turning on and off Particle effects 2 Answers
Android: FPS halt when doing things for the first time 2 Answers
Immortal enemies when reanimated, or pooled game objects re-activated do not respond to collisions? 1 Answer