125 lines
3.6 KiB
C#
125 lines
3.6 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
public class MusicPlayer
|
|
{
|
|
private AudioSourcePool audioSourcePool;
|
|
private AudioSource audioSource1;
|
|
private AudioSource audioSource2;
|
|
private AudioSource currentAudioSource;
|
|
|
|
public MusicPlayer(AudioSourcePool audioSourcePool)
|
|
{
|
|
this.audioSourcePool = audioSourcePool;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 播放背景音乐
|
|
/// </summary>
|
|
/// <param name="clip">音频</param>
|
|
/// <param name="volume">音量</param>
|
|
/// <param name="fadeDuration">自然过渡时间</param>
|
|
public void Play(AudioClip clip, float volume, float fadeDuration)
|
|
{
|
|
if(audioSource1 == null)
|
|
{
|
|
audioSource1 = audioSourcePool.GetAudioSource("Music");
|
|
audioSource1.clip = clip;
|
|
audioSource1.volume = volume;
|
|
currentAudioSource = audioSource1;
|
|
currentAudioSource.Play();
|
|
CoroutineHelper.StartCoroutine(FadeMusic(audioSource1, fadeDuration, audioSource2));
|
|
}
|
|
else
|
|
{
|
|
if (audioSource2 == null)
|
|
{
|
|
audioSource2 = audioSourcePool.GetAudioSource("Music");
|
|
audioSource2.clip = clip;
|
|
audioSource2.volume = volume;
|
|
currentAudioSource = audioSource2;
|
|
currentAudioSource.Play();
|
|
CoroutineHelper.StartCoroutine(FadeMusic(audioSource2, fadeDuration, audioSource1));
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("已同时存在两个背景乐在切换");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭背景音乐
|
|
/// </summary>
|
|
/// <param name="fadeDuration">关闭时的自然过渡时间</param>
|
|
public void Stop(float fadeDuration)
|
|
{
|
|
CoroutineHelper.StartCoroutine(FadeOutMusic(currentAudioSource, fadeDuration));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换音频
|
|
/// </summary>
|
|
/// <param name="source1">播放的音频</param>
|
|
/// <param name="fadeDuration">变化时间</param>
|
|
/// <param name="source2">停止的音频</param>
|
|
/// <returns></returns>
|
|
private IEnumerator FadeMusic(AudioSource source1, float fadeDuration, AudioSource source2 = null)
|
|
{
|
|
yield return FadeInMusic(source1, fadeDuration);
|
|
|
|
if(source2 != null)
|
|
{
|
|
yield return FadeOutMusic(source2, fadeDuration);
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭音频的协程
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="fadeDuration"></param>
|
|
/// <returns></returns>
|
|
private IEnumerator FadeOutMusic(AudioSource source, float fadeDuration)
|
|
{
|
|
float startVolume = source.volume;
|
|
|
|
while (source.volume > 0)
|
|
{
|
|
source.volume -= startVolume * Time.deltaTime / fadeDuration;
|
|
yield return null;
|
|
}
|
|
|
|
source.Stop();
|
|
audioSourcePool.ReturnAudioSource("Music", source.gameObject);
|
|
|
|
if(currentAudioSource == audioSource1)
|
|
{
|
|
audioSource2 = null;
|
|
}
|
|
else if(currentAudioSource == audioSource2)
|
|
{
|
|
audioSource1 = null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 开启音频的协程
|
|
/// </summary>
|
|
/// <param name="source"></param>
|
|
/// <param name="fadeDuration"></param>
|
|
/// <returns></returns>
|
|
private IEnumerator FadeInMusic(AudioSource source, float fadeDuration)
|
|
{
|
|
float targetVolume = source.volume;
|
|
source.volume = 0;
|
|
|
|
while (source.volume < targetVolume)
|
|
{
|
|
source.volume += targetVolume * Time.deltaTime / fadeDuration;
|
|
yield return null;
|
|
}
|
|
}
|
|
} |