From 702a7fd26244c8d4c52c9f2771b80d06e2321586 Mon Sep 17 00:00:00 2001
From: Han <1985708279@qq.com>
Date: Wed, 7 May 2025 14:34:07 +0800
Subject: [PATCH] =?UTF-8?q?=E3=80=90a=E3=80=91=E6=B7=BB=E5=8A=A0=E8=AE=BE?=
=?UTF-8?q?=E7=BD=AE=E8=83=8C=E6=99=AF=E9=9F=B3=E4=B9=90=E9=9F=B3=E9=87=8F?=
=?UTF-8?q?=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../04.AudioCore/RunTime/Base/MusicPlayer.cs | 45 +++++++++++++++++++
.../RunTime/Use/AudioCoreManager.cs | 12 +++++
Assets/04.AudioCore/package.json | 2 +-
3 files changed, 58 insertions(+), 1 deletion(-)
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",