【a】添加设置背景音乐音量功能
This commit is contained in:
@@ -9,6 +9,7 @@ namespace Stary.Evo.AudioCore
|
|||||||
private AudioSource audioSource1;
|
private AudioSource audioSource1;
|
||||||
private AudioSource audioSource2;
|
private AudioSource audioSource2;
|
||||||
private AudioSource currentAudioSource;
|
private AudioSource currentAudioSource;
|
||||||
|
private Coroutine myCoroutine;
|
||||||
|
|
||||||
public MusicPlayer(AudioSourcePool audioSourcePool)
|
public MusicPlayer(AudioSourcePool audioSourcePool)
|
||||||
{
|
{
|
||||||
@@ -61,6 +62,50 @@ namespace Stary.Evo.AudioCore
|
|||||||
FadeAllMusic();
|
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>
|
||||||
/// 切换音频
|
/// 切换音频
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Security.Cryptography.X509Certificates;
|
||||||
|
|
||||||
namespace Stary.Evo.AudioCore
|
namespace Stary.Evo.AudioCore
|
||||||
{
|
{
|
||||||
@@ -78,6 +79,17 @@ namespace Stary.Evo.AudioCore
|
|||||||
Music.Play(audioData);
|
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>
|
||||||
/// 停止播放背景音乐
|
/// 停止播放背景音乐
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "com.staryevo.audiocore",
|
"name": "com.staryevo.audiocore",
|
||||||
"version": "1.0.8",
|
"version": "1.0.9",
|
||||||
"displayName": "04.AudioCore",
|
"displayName": "04.AudioCore",
|
||||||
"description": "音频播放工具",
|
"description": "音频播放工具",
|
||||||
"unity": "2021.3",
|
"unity": "2021.3",
|
||||||
|
|||||||
Reference in New Issue
Block a user