Skip to content

Commit e6d2a93

Browse files
authored
feat: audio settings (#36)
1 parent f771ed2 commit e6d2a93

File tree

13 files changed

+188
-31
lines changed

13 files changed

+188
-31
lines changed

Assets/Audio/Audio Manager.asset

Lines changed: 0 additions & 18 deletions
This file was deleted.

Assets/Audio/AudioManager.cs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,63 @@
11
using FMOD.Studio;
22
using FMODUnity;
3-
using System;
3+
using Settings.Bundles;
44
using UnityEngine;
55
using UnityEngine.SceneManagement;
6+
using Utils;
67

78
namespace Audio {
8-
public class AudioManager : ScriptableObject {
9+
public class AudioManager : MonoBehaviour {
10+
[Inject] [SerializeField] private AudioSettingsBundle _bundle;
11+
912
[SerializeField] private FMODEventInstance _ambienceAudio;
1013
[SerializeField] private FMODParameterInstance _ambienceSceneParam;
1114

15+
private VCA _masterVca;
16+
private VCA _musicVca;
17+
private VCA _sfxVca;
18+
1219
private void OnEnable() {
13-
#if UNITY_EDITOR
14-
if (!Application.isPlaying) {
15-
return;
16-
}
17-
#endif
1820
_ambienceAudio.Setup();
1921
_ambienceSceneParam.Setup();
22+
23+
RuntimeManager.StudioSystem.getVCA("vca:/Master", out _masterVca);
24+
RuntimeManager.StudioSystem.getVCA("vca:/Music", out _musicVca);
25+
RuntimeManager.StudioSystem.getVCA("vca:/SFX", out _sfxVca);
26+
27+
_bundle.MasterVolume.Changed += HandleMasterVolumeChanged;
28+
_bundle.MusicVolume.Changed += HandleMusicVolumeChanged;
29+
_bundle.SfxVolume.Changed += HandleSfxVolumeChanged;
2030
}
2131

2232
private void OnDisable() {
2333
_ambienceAudio.Release();
34+
35+
_bundle.MasterVolume.Changed -= HandleMasterVolumeChanged;
36+
_bundle.MusicVolume.Changed -= HandleMusicVolumeChanged;
37+
_bundle.MusicVolume.Changed -= HandleMusicVolumeChanged;
2438
}
2539

2640
public void PlayAmbience() {
2741
if (_ambienceAudio.IsInitialized) {
28-
_ambienceSceneParam.CurrentValue = SceneManager.GetActiveScene().buildIndex;
42+
_ambienceSceneParam.CurrentValue =
43+
SceneManager.GetActiveScene().buildIndex;
2944
_ambienceAudio.Instance.getPlaybackState(out var state);
3045
if (state == PLAYBACK_STATE.STOPPED) {
3146
_ambienceAudio.Play();
3247
}
3348
}
3449
}
50+
51+
private void HandleMasterVolumeChanged(int value) {
52+
_masterVca.setVolume(value / 100f);
53+
}
54+
55+
private void HandleMusicVolumeChanged(int value) {
56+
_musicVca.setVolume(value / 100f);
57+
}
58+
59+
private void HandleSfxVolumeChanged(int value) {
60+
_sfxVca.setVolume(value / 100f);
61+
}
3562
}
3663
}

Assets/Framework/GameManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Framework {
1010
public class GameManager : MonoBehaviour {
1111
[Inject] public MenuMode MenuMode;
1212
[Inject] public StoryMode StoryMode;
13-
[Inject] public AudioManager Audio;
13+
public AudioManager Audio;
1414

1515
private GameMode _currentMode;
1616
private GameMode _switchingTo;

Assets/Scenes/App.unity

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Settings/Bundles/AudioSettingsBundle.asset

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Audio/Audio Manager.asset.meta renamed to Assets/Settings/Bundles/AudioSettingsBundle.asset.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using UnityEngine;
2+
3+
namespace Settings.Bundles {
4+
public class AudioSettingsBundle : ScriptableObject {
5+
public SettingProperty MasterVolume;
6+
public SettingProperty MusicVolume;
7+
public SettingProperty SfxVolume;
8+
9+
private void OnEnable() {
10+
MasterVolume = new SettingProperty("masterVolume", 100);
11+
MusicVolume = new SettingProperty("musicVolume", 100);
12+
SfxVolume = new SettingProperty("sfxVolume", 100);
13+
}
14+
}
15+
}

Assets/Settings/Bundles/AudioSettingsBundle.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/View/Prefabs/Settings.prefab

Lines changed: 64 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Settings.Bundles;
2+
using UnityEngine;
3+
using Utils;
4+
using View.Controls;
5+
6+
namespace View.Settings {
7+
[DefaultExecutionOrder(100)]
8+
public class AudioSettingsView : MonoBehaviour {
9+
[Inject] [SerializeField] private AudioSettingsBundle _bundle;
10+
11+
[SerializeField] private PaperSlider _masterVolume;
12+
[SerializeField] private PaperSlider _musicVolume;
13+
[SerializeField] private PaperSlider _sfxVolume;
14+
15+
private void OnEnable() {
16+
_masterVolume.Value = _bundle.MasterVolume.Get();
17+
_musicVolume.Value = _bundle.MusicVolume.Get();
18+
_sfxVolume.Value = _bundle.SfxVolume.Get();
19+
20+
_masterVolume.ValueChanged += _bundle.MasterVolume.Set;
21+
_musicVolume.ValueChanged += _bundle.MusicVolume.Set;
22+
_sfxVolume.ValueChanged += _bundle.SfxVolume.Set;
23+
}
24+
25+
private void OnDisable() {
26+
_masterVolume.ValueChanged -= _bundle.MasterVolume.Set;
27+
_musicVolume.ValueChanged -= _bundle.MusicVolume.Set;
28+
_sfxVolume.ValueChanged -= _bundle.SfxVolume.Set;
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)