Files
cocos4/cocos/3d/framework/particle/particle-utils.ts
T
Yun Hsiao Wu 9711e049ba skeletal socket system (#5004)
* skeletal socket system

* fix
2019-07-29 19:45:24 +08:00

58 lines
1.8 KiB
TypeScript

/**
* @hidden
*/
import { ParticleSystemComponent } from '..';
import { CCObject } from '../../../core/data/object';
import { Node } from '../../../scene-graph';
import { Pool } from '../../memop';
export class ParticleUtils {
private static particleSystemPool: Map<string, Pool<CCObject>> = new Map<string, Pool<CCObject>>();
private static registeredSceneEvent: boolean = false;
/**
* instantiate
*/
public static instantiate (prefab) {
if (!this.registeredSceneEvent) {
cc.director.on(cc.Director.EVENT_BEFORE_SCENE_LAUNCH, this.onSceneUnload, this);
this.registeredSceneEvent = true;
}
if (!this.particleSystemPool.has(prefab._uuid)) {
this.particleSystemPool.set(prefab._uuid, new Pool<CCObject>(() => {
return cc.instantiate(prefab);
}, 1));
}
return this.particleSystemPool.get(prefab._uuid)!.alloc();
}
public static destroy (prefab) {
if (this.particleSystemPool.has(prefab._prefab.asset._uuid)) {
this.stop(prefab);
this.particleSystemPool.get(prefab._prefab.asset._uuid)!.free(prefab);
}
}
private static onSceneUnload () {
this.particleSystemPool.forEach((value) => {
value.clear((prefab) => {
prefab.destroy();
});
});
this.particleSystemPool.clear();
}
public static play (rootNode: Node) {
for (const ps of rootNode.getComponentsInChildren(cc.ParticleSystemComponent)) {
(ps as ParticleSystemComponent).play();
}
}
public static stop (rootNode: Node) {
for (const ps of rootNode.getComponentsInChildren(cc.ParticleSystemComponent)) {
(ps as ParticleSystemComponent).stop();
}
}
}