using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Audio; public class SoundManager : MonoBehaviour { public AudioMixer audioMixer; float masterVol; float musicVol; float sfxVol; [SerializeField]Slider masterSlider; [SerializeField]Slider musicSlider; [SerializeField]Slider sfxSlider; void Awake() { masterVol = PlayerPrefs.GetFloat("MasterVolume", masterSlider.maxValue); musicVol = PlayerPrefs.GetFloat("MusicVolume", musicSlider.maxValue); sfxVol = PlayerPrefs.GetFloat("SFXVolume", sfxSlider.maxValue); masterSlider.value = masterVol; musicSlider.value = musicVol; sfxSlider.value = sfxVol; masterSlider.onValueChanged.AddListener((float _) => SetMasterVolume(_)); musicSlider.onValueChanged.AddListener((float _) => SetMusicVolume(_)); sfxSlider.onValueChanged.AddListener((float _) => SetSFXVolume(_)); } void Start() { SetMasterVolume(masterVol); SetMusicVolume(musicVol); SetSFXVolume(sfxVol); } public void PlaySound(AudioSource source, bool randomPitch=false, bool createTempSourceIfBusy=true, bool createTempSource = false, float delay = 0f) { if (source == null || !source.gameObject.activeInHierarchy) { return; } if (source.isPlaying) { if (createTempSourceIfBusy) { if (randomPitch) { source.pitch = Random.Range(0.9f, 1.1f); } StartCoroutine(CreateTempSource(source, delay:delay)); } } else { if (randomPitch) { source.pitch = Random.Range(0.9f, 1.1f); } if(createTempSource) { StartCoroutine(CreateTempSource(source, delay: delay)); } else { if(delay > 0f) { StartCoroutine(DelayedPlaySound(source, delay)); } else { source.Play(); } } } } public void PlaySound(AudioSource source, AudioClip[] possibleClips, bool randomPitch=false, bool createTempSourceIfBusy=true, bool createTempSource=false, float delay=0f) { if(source == null || ! source.gameObject.activeInHierarchy || possibleClips.Length == 0) { return; } source.clip = possibleClips[Random.Range(0, possibleClips.Length)]; PlaySound(source, randomPitch, createTempSourceIfBusy, createTempSource, delay); } IEnumerator CreateTempSource(AudioSource refSource, float delay=0f) { AudioSource newSource = refSource.gameObject.AddComponent(); newSource.volume = refSource.volume; newSource.clip = refSource.clip; newSource.spatialize = refSource.spatialize; newSource.spatialBlend = refSource.spatialBlend; PlaySound(newSource, delay:delay); yield return new WaitForSeconds(delay); // yield return new WaitForSeconds(1f); // Destroy(newSource); for(int i=0; i<100; ++i) { yield return new WaitForSeconds(1f); if(! newSource.isPlaying) { Destroy(newSource); break; } } } public void StopSound(AudioSource source) { source.Stop(); } IEnumerator DelayedPlaySound(AudioSource source, float delay) { yield return new WaitForSeconds(delay); source.Play(); } public void SetMasterVolume(float value) { // Slider should go from 0.0001 to 1 masterVol = value; audioMixer.SetFloat("MasterVolume", Mathf.Log10(masterVol) * 20); PlayerPrefs.SetFloat("MasterVolume", masterVol); } public void SetMusicVolume(float value) { // Slider should go from 0.0001 to 1 musicVol = value; audioMixer.SetFloat("MusicVolume", Mathf.Log10(musicVol) * 20); PlayerPrefs.SetFloat("MusicVolume", musicVol); } public void SetSFXVolume(float value) { // Slider should go from 0.0001 to 1 sfxVol = value; audioMixer.SetFloat("SFXVolume", Mathf.Log10(sfxVol) * 20); PlayerPrefs.SetFloat("SFXVolume", sfxVol); } public float GetMasterVolume() { return masterVol; } public float GetMusicVolume() { return musicVol; } public float GetSFXVolume() { return sfxVol; } }