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