【a】添加设置背景音乐音量功能
This commit is contained in:
@@ -9,6 +9,7 @@ namespace Stary.Evo.AudioCore
|
||||
private AudioSource audioSource1;
|
||||
private AudioSource audioSource2;
|
||||
private AudioSource currentAudioSource;
|
||||
private Coroutine myCoroutine;
|
||||
|
||||
public MusicPlayer(AudioSourcePool audioSourcePool)
|
||||
{
|
||||
@@ -61,6 +62,50 @@ namespace Stary.Evo.AudioCore
|
||||
FadeAllMusic();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置背景音乐音量
|
||||
/// </summary>
|
||||
/// <param name="audioData"></param>
|
||||
public void SetMusicVolume(AudioData audioData)
|
||||
{
|
||||
if(currentAudioSource == null) return;
|
||||
|
||||
if (myCoroutine != null)
|
||||
{
|
||||
CoroutineHelper.StopCoroutine(myCoroutine);
|
||||
myCoroutine = null;
|
||||
}
|
||||
myCoroutine = CoroutineHelper.StartCoroutine(SetMusicVolume(audioData.fadeDuration, audioData.volume));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置自然过渡到指定音量
|
||||
/// </summary>
|
||||
/// <param name="fadeDuration"></param>
|
||||
/// <param name="targetVolume"></param>
|
||||
/// <returns></returns>
|
||||
private IEnumerator SetMusicVolume(float fadeDuration, float targetVolume)
|
||||
{
|
||||
float startVolume = currentAudioSource.volume;
|
||||
while (currentAudioSource.volume != targetVolume)
|
||||
{
|
||||
if (currentAudioSource.volume > targetVolume)
|
||||
{
|
||||
currentAudioSource.volume -= startVolume * Time.deltaTime / fadeDuration;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentAudioSource.volume += startVolume * Time.deltaTime / fadeDuration;
|
||||
}
|
||||
|
||||
if (Mathf.Abs(currentAudioSource.volume - targetVolume) < 0.01f)
|
||||
{
|
||||
currentAudioSource.volume = targetVolume;
|
||||
}
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 切换音频
|
||||
/// </summary>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace Stary.Evo.AudioCore
|
||||
{
|
||||
@@ -78,6 +79,17 @@ namespace Stary.Evo.AudioCore
|
||||
Music.Play(audioData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置背景音乐音量
|
||||
/// </summary>
|
||||
/// <param name="audioData">{[volume:音量], [fadeDuration:自然过渡时间]}</param>
|
||||
public static void SetMusicVolume(AudioData audioData)
|
||||
{
|
||||
if(CoroutineHelper.coroutineRunner == null) return;
|
||||
Music.SetMusicVolume(audioData);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 停止播放背景音乐
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user