Cocos Creator

ParticleSystem, Animation 재시작

밀하우스 마나스톰 2022. 3. 18. 18:06

Fx/effect_smoke에 파티클 컴포넌트가 붙어있다.

 

그리고 effect_smoke 파티클은 Loop가 해제돼있어서 한 번만 재생되고

 

Play On Awake 옵션으로 인해 노드가 활성화될 때 재생된다.

 

위와 같은 상황에서 effect_smoke 노드 또는 부모 노드인 Fx를  껐다가 다시 키면

 

effect_smoke는 이전 상태(이전 frame)를 이어서 재생한다. 노드가 꺼졌을 때 stop이 아니라 resume인 것이다.

 

이러한 성질은 cc.ParticleSystem 뿐만 아니라 cc.Animation에도 동일하게 적용된다.

 

유니티의의 경우 오브젝트를 껐다 켰을 때 Animation을 다시 처음부터 재생한다. 코코스와 유니티의 차이다.

 

 

어떤 이펙트를 껐다가 다시 켰을 때는 처음부터 다시 재생하도록 원하는 경우일 가능성이 높다.

 

노드를 다시 켰을 때 첫 frame부터 재생하도록 하는 코드를 추가해도 되지만,

 

위와 같은 상황은 워낙 자주 발생하고, 애니메이션 컴포넌트나 커스텀 컴포넌트도 동일한 문제가 발생할 수 있다.

 

 

결론은 파티클, 애니메이션 컴포넌트가 붙어있는 노드의 active가 꺼질 때

 

자동으로 파티클, 애니메이션을 stop 시키는 기능이 필요하다.

 

 

const {ccclass} = cc._decorator;

@ccclass
export default class DisableReset extends cc.Component {
    animations: Array<cc.Animation>;
    particles: Array<cc.ParticleSystem3D>;

    onLoad() {
        this.animations = this.node.getComponentsInChildren(cc.Animation);
        this.particles = this.node.getComponentsInChildren(cc.ParticleSystem3D);
    }

    onDisable() {
        this.animations.forEach(x => x.stop());
        this.particles.forEach(x => x.stop());
    }
}

간단하게 위와 같은 컴포넌트를 만들고 Fx 노드에 붙여주면 된다.