From f075f6ef65a23b8b714fa7d11d274edbf9fd38e4 Mon Sep 17 00:00:00 2001 From: dgflash Date: Sat, 9 Mar 2024 15:11:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=9F=B3=E4=B9=90=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E6=9C=AC=E5=9C=B0=E6=95=B0=E6=8D=AE=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E6=8F=90=E4=BE=9B=E4=B8=80=E5=A5=97=E9=BB=98?= =?UTF-8?q?=E8=AE=A4=E6=95=B0=E6=8D=AE=E9=81=BF=E5=85=8D=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/core/common/audio/AudioManager.ts | 43 ++++++++++++++---------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/assets/core/common/audio/AudioManager.ts b/assets/core/common/audio/AudioManager.ts index 32ff136..3d667a7 100644 --- a/assets/core/common/audio/AudioManager.ts +++ b/assets/core/common/audio/AudioManager.ts @@ -167,8 +167,7 @@ export class AudioManager extends Component { this.local_data.switch_music = this._switch_music; this.local_data.switch_effect = this._switch_effect; - let data = JSON.stringify(this.local_data); - oops.storage.set(LOCAL_STORE_KEY, data); + oops.storage.set(LOCAL_STORE_KEY, this.local_data); } @@ -177,25 +176,35 @@ export class AudioManager extends Component { this.music = this.getComponent(AudioMusic) || this.addComponent(AudioMusic)!; this.effect = this.getComponent(AudioEffect) || this.addComponent(AudioEffect)!; - let data = oops.storage.get(LOCAL_STORE_KEY); - if (data) { + this.local_data = oops.storage.getJson(LOCAL_STORE_KEY); + if (this.local_data) { try { - this.local_data = JSON.parse(data); - this._volume_music = this.local_data.volume_music; - this._volume_effect = this.local_data.volume_effect; - this._switch_music = this.local_data.switch_music; - this._switch_effect = this.local_data.switch_effect; + this.setState(); } catch (e) { - this.local_data = {}; - this._volume_music = 1; - this._volume_effect = 1; - this._switch_music = true; - this._switch_effect = true; + this.setStateDefault(); } - - if (this.music) this.music.volume = this._volume_music; - if (this.effect) this.effect.volume = this._volume_effect; } + else { + this.setStateDefault(); + } + + if (this.music) this.music.volume = this._volume_music; + if (this.effect) this.effect.volume = this._volume_effect; + } + + private setState() { + this._volume_music = this.local_data.volume_music; + this._volume_effect = this.local_data.volume_effect; + this._switch_music = this.local_data.switch_music; + this._switch_effect = this.local_data.switch_effect; + } + + private setStateDefault() { + this.local_data = {}; + this._volume_music = 1; + this._volume_effect = 1; + this._switch_music = true; + this._switch_effect = true; } } \ No newline at end of file