using UnityEngine; using UnityEngine.UI; public class SettingsManager : MonoBehaviour { public static SettingsManager Instance { get; set; } /// /// The given music volume slider element /// public Slider musicSlider; /// /// The given sfx volume slider element /// public Slider sfxSlider; private Settings settings; void Awake() { Instance = this; } void Start() { ApplySettings(GameManager.Instance.settings); } /// /// Sets the musics volume for whole environment and applies volume to global settings /// /// public void SetMusicsVolume(float volume) { SetMusicsVolumeSoundSystem(volume); //Changes settings settings.audio.MusicsVolume = volume; GameManager.Instance.ApplySettings(settings); } /// /// Sets the sfx volume for whole environment and applies volume to global settings /// /// public void SetSFXVolume(float volume) { SetSFXVolumeSoundSystem(volume); //Changes settings settings.audio.SfxVolume = volume; GameManager.Instance.ApplySettings(settings); } private void SetMusicsVolumeSoundSystem(float volume) { AudioManager.Instance.SetMusicsVolume(volume); Debug.Log("Music volume changed: " + volume.ToString("0.00")); } private void SetSFXVolumeSoundSystem(float volume) { AudioManager.Instance.SetSFXVolume(volume); Debug.Log("SFX volume changed: " + volume.ToString("0.00")); } /// /// Applies given settings to global settings and executes them /// /// public void ApplySettings(Settings settings) { this.settings = settings; ApplyAudioSettings(settings.audio); Debug.Log("All settings successfully applied"); } private void ApplyAudioSettings(Settings.Audio settings) { float music = settings.MusicsVolume; float sfx = settings.SfxVolume; if (musicSlider.value == music) { musicSlider.value = music; SetMusicsVolumeSoundSystem(music); } else { musicSlider.value = music; } if (sfxSlider.value == sfx) { sfxSlider.value = sfx; SetSFXVolumeSoundSystem(sfx); } else { sfxSlider.value = sfx; } Debug.Log("Applied sound settings: Music=" + settings.MusicsVolume.ToString("0.00") + " SFX=" + settings.SfxVolume.ToString("0.00")); } }