using System.Collections; using System.Collections.Generic; using UnityEngine; public class SoundPlayer : MonoBehaviour { public List musics; public List sfx; public AudioSource thisAS; public static SoundPlayer instance; // Start is called before the first frame update void Awake() { instance = this; thisAS = GetComponent(); thisAS.clip = musics[0]; thisAS.volume = .5f; thisAS.Play(); } public void PlaySong(int i) { StartCoroutine(ChangeMusic(i)); } public void PlaySFX(int i) { thisAS.PlayOneShot(sfx[i], .7f); } public void ToggleMute() { thisAS.volume = thisAS.volume == 0 ? .5f : 0; } IEnumerator Fadein() { for (float i = 0; i < .5f; i += .05f) { thisAS.volume = i; yield return new WaitForSecondsRealtime(.1f); } thisAS.volume = .5f; } IEnumerator FadeOut() { for (float i = .5f; i > 0f; i -= .05f) { thisAS.volume = i; yield return new WaitForSecondsRealtime(.1f); } thisAS.volume = 0f; } IEnumerator ChangeMusic(int i) { yield return StartCoroutine(FadeOut()); thisAS.clip = musics[i]; thisAS.Play(); yield return StartCoroutine(Fadein()); } }