SoundConfigurationsUI.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.Audio;
  6. using Sirenix.OdinInspector;
  7. using KairoEngine.Core;
  8. namespace KairoEngine.UI
  9. {
  10. public class SoundConfigurationsUI : MonoBehaviour
  11. {
  12. public AudioMixerGroup masterMixer;
  13. public Slider masterVolumeSlider;
  14. public string masterVolumeName = "masterVolume";
  15. public Slider musicVolumeSlider;
  16. public string musicVolumeName = "musicVolume";
  17. public Slider sfxVolumeSlider;
  18. public string sfxVolumeName = "sfxVolume";
  19. float masterVolume = 1;
  20. float musicVolume = 1;
  21. float sfxVolume = 1;
  22. Coroutine delayedSave;
  23. public void OnEnable()
  24. {
  25. masterVolumeSlider.onValueChanged.AddListener(delegate {UpdateMasterVolume(); });
  26. musicVolumeSlider.onValueChanged.AddListener(delegate {UpdateMusicVolume(); });
  27. sfxVolumeSlider.onValueChanged.AddListener(delegate {UpdateSfxVolume(); });
  28. }
  29. void OnDisable()
  30. {
  31. masterVolumeSlider.onValueChanged.RemoveListener(delegate {UpdateMasterVolume(); });
  32. musicVolumeSlider.onValueChanged.RemoveListener(delegate {UpdateMusicVolume(); });
  33. sfxVolumeSlider.onValueChanged.RemoveListener(delegate {UpdateSfxVolume(); });
  34. }
  35. private void UpdateMasterVolume() => UpdateVolume(masterVolumeName, masterVolumeSlider, ref masterVolume);
  36. private void UpdateMusicVolume() => UpdateVolume(musicVolumeName, musicVolumeSlider, ref musicVolume);
  37. private void UpdateSfxVolume() => UpdateVolume(sfxVolumeName, sfxVolumeSlider, ref sfxVolume);
  38. void Start() => LoadConfig();
  39. void UpdateVolume(string volumeName, Slider slider, ref float volume)
  40. {
  41. masterMixer.audioMixer.SetFloat(volumeName, Mathf.Log10(slider.value) * 20);
  42. volume = slider.value;
  43. if(delayedSave != null) StopCoroutine(delayedSave);
  44. delayedSave = StartCoroutine(Timer.StartRealtime(2f, SaveConfig));
  45. }
  46. void LoadConfig()
  47. {
  48. masterVolume = PlayerPrefs.GetFloat("MasterVolume", masterVolume);
  49. musicVolume = PlayerPrefs.GetFloat("MusicVolume", musicVolume);
  50. sfxVolume = PlayerPrefs.GetFloat("SFXVolume", sfxVolume);
  51. masterVolumeSlider.value = masterVolume;
  52. musicVolumeSlider.value = musicVolume;
  53. sfxVolumeSlider.value = sfxVolume;
  54. masterMixer.audioMixer.SetFloat(masterVolumeName, Mathf.Log10(masterVolume) * 20);
  55. masterMixer.audioMixer.SetFloat(musicVolumeName, Mathf.Log10(musicVolume) * 20);
  56. masterMixer.audioMixer.SetFloat(sfxVolumeName, Mathf.Log10(sfxVolume) * 20);
  57. }
  58. void SaveConfig()
  59. {
  60. PlayerPrefs.SetFloat("MasterVolume", masterVolume);
  61. PlayerPrefs.SetFloat("MusicVolume", musicVolume);
  62. PlayerPrefs.SetFloat("SFXVolume", sfxVolume);
  63. Debug.Log($"Master: {masterVolume} | Music: {musicVolume} | SFX: {sfxVolume}");
  64. }
  65. }
  66. }