using UnityEngine; using UnityEngine.UI; public class SoundManager : MonoBehaviour { public static SoundManager Instance { get; private set; } [SerializeField] Slider volumeSlider; public const float defaultVolume = 0.5f; void Start() { if (!PlayerPrefs.HasKey("soundVolume")) PlayerPrefs.SetFloat("soundVolume", defaultVolume); Load(); } public void Awake() { //singleton if (Instance == null) { Instance = this; if (gameObject.IsRoot()) DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } } private void OnDestroy() { Instance = null; } public void ChangeVolume() { AudioListener.volume = volumeSlider.value; Save(); } private void Load() { volumeSlider.value = PlayerPrefs.GetFloat("soundVolume"); } private void Save() { PlayerPrefs.SetFloat("soundVolume", volumeSlider.value); } }