diff --git a/Assets/04.AudioCore/RunTime/Base/MusicPlayer.cs b/Assets/04.AudioCore/RunTime/Base/MusicPlayer.cs index 742fe60..affb413 100644 --- a/Assets/04.AudioCore/RunTime/Base/MusicPlayer.cs +++ b/Assets/04.AudioCore/RunTime/Base/MusicPlayer.cs @@ -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(); } + /// + /// 设置背景音乐音量 + /// + /// + 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)); + } + + /// + /// 设置自然过渡到指定音量 + /// + /// + /// + /// + 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; + } + } + /// /// 切换音频 /// diff --git a/Assets/04.AudioCore/RunTime/Use/AudioCoreManager.cs b/Assets/04.AudioCore/RunTime/Use/AudioCoreManager.cs index 01093cf..c836877 100644 --- a/Assets/04.AudioCore/RunTime/Use/AudioCoreManager.cs +++ b/Assets/04.AudioCore/RunTime/Use/AudioCoreManager.cs @@ -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); } + /// + /// 设置背景音乐音量 + /// + /// {[volume:音量], [fadeDuration:自然过渡时间]} + public static void SetMusicVolume(AudioData audioData) + { + if(CoroutineHelper.coroutineRunner == null) return; + Music.SetMusicVolume(audioData); + } + + /// /// 停止播放背景音乐 /// diff --git a/Assets/04.AudioCore/package.json b/Assets/04.AudioCore/package.json index 5bb8036..9e2afe4 100644 --- a/Assets/04.AudioCore/package.json +++ b/Assets/04.AudioCore/package.json @@ -1,6 +1,6 @@ { "name": "com.staryevo.audiocore", - "version": "1.0.8", + "version": "1.0.9", "displayName": "04.AudioCore", "description": "音频播放工具", "unity": "2021.3",