91 lines
1.9 KiB
C#
91 lines
1.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class LGB_SoundPlayer : MonoBehaviour
|
|
{
|
|
public static LGB_SoundPlayer instance;
|
|
public List<AudioClip> sfxs;
|
|
public List<AudioClip> songs;
|
|
public List<Sprite> buttonSprites;
|
|
public float volume;
|
|
|
|
private Button muteButton;
|
|
private AudioSource thisAS;
|
|
|
|
private void Awake()
|
|
{
|
|
muteButton = GetComponentInChildren<Button>();
|
|
instance = this;
|
|
thisAS = GetComponent<AudioSource>();
|
|
if (songs.Count > 0 )
|
|
{
|
|
thisAS.clip = songs[0];
|
|
thisAS.Play();
|
|
}
|
|
muteButton.onClick.AddListener(ToggleMute);
|
|
|
|
}
|
|
|
|
public void ToggleMute()
|
|
{
|
|
if (thisAS.volume == 0)
|
|
{
|
|
thisAS.volume = volume;
|
|
muteButton.image.sprite = buttonSprites[0];
|
|
}
|
|
else
|
|
{
|
|
thisAS.volume = 0;
|
|
muteButton.image.sprite = buttonSprites[1];
|
|
}
|
|
}
|
|
|
|
public void PlaySfx(int n)
|
|
{
|
|
thisAS.PlayOneShot(sfxs[n]);
|
|
}
|
|
|
|
public void PlayMusic(int n)
|
|
{
|
|
if (thisAS.isPlaying)
|
|
{
|
|
DoChangeMusic(n);
|
|
}
|
|
else
|
|
{
|
|
thisAS.clip = songs[n];
|
|
thisAS.Play();
|
|
}
|
|
}
|
|
|
|
IEnumerator DoChangeMusic(int n)
|
|
{
|
|
yield return DoFadeOut();
|
|
thisAS.clip = songs[n];
|
|
thisAS.Play();
|
|
yield return DoFadeIn();
|
|
}
|
|
|
|
IEnumerator DoFadeIn()
|
|
{
|
|
for (float i = 0; i < volume; i+= 0.1f)
|
|
{
|
|
thisAS.volume = i;
|
|
yield return new WaitForSecondsRealtime(.1f);
|
|
}
|
|
thisAS.volume = volume;
|
|
}
|
|
|
|
IEnumerator DoFadeOut()
|
|
{
|
|
for (float i = volume; i > 0; i += 0.1f)
|
|
{
|
|
thisAS.volume = i;
|
|
yield return new WaitForSecondsRealtime(.1f);
|
|
}
|
|
thisAS.volume = 0;
|
|
}
|
|
}
|