音效管理优化

This commit is contained in:
dgflash
2025-08-28 20:29:58 +08:00
parent 3e47b3daa6
commit e94f265cb8
2 changed files with 14 additions and 9 deletions

View File

@@ -4,8 +4,7 @@
* @LastEditors: dgflash
* @LastEditTime: 2022-09-02 10:22:36
*/
import { AudioClip } from 'cc';
import { AudioSource, _decorator } from 'cc';
import { AudioClip, AudioSource, _decorator } from 'cc';
import { IAudioParams } from './IAudio';
const { ccclass } = _decorator;
@@ -30,4 +29,11 @@ export class AudioEffect extends AudioSource {
private onAudioEnded() {
this.onComplete && this.onComplete(this);
}
reset() {
this.stop();
this.clip = null;
this.path = null!;
this.params = null!;
}
}

View File

@@ -168,28 +168,27 @@ export class AudioEffectPool {
key += "_" + aeid;
node = new Node("AudioEffect");
ae = node.addComponent(AudioEffect);
ae.key = key;
ae.aeid = aeid;
ae.path = path;
ae.params = params;
ae.onComplete = this.onAudioEffectPlayComplete.bind(this);
}
else {
node = this.pool.get()!;
ae = node.getComponent(AudioEffect)!;
key = ae.key;
}
// 记录正在播放的音效播放器
this.effects.set(key, ae);
this.effects.set(ae.key, ae);
node.parent = oops.audio.node;
ae.path = path;
ae.params = params;
ae.loop = params.loop!;
ae.volume = params.volume!;
ae.clip = clip;
ae.play();
resolve(ae);
});
}
@@ -208,8 +207,8 @@ export class AudioEffectPool {
put(ae: AudioEffect) {
let effect = this.effects.get(ae.key);
if (effect && effect.clip) {
effect.stop();
effect.clip = null;
effect.reset();
this.effects.delete(ae.key);
this.pool.put(effect.node);
}