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