1111
This commit is contained in:
@@ -1,19 +1,29 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace Stary.Evo.AudioCore
|
||||
{
|
||||
public class MusicPlayer : AbstractAudio
|
||||
public class MusicPlayer : AbstractAudio, IDisposable
|
||||
{
|
||||
private AudioSourcePool audioSourcePool;
|
||||
private GameObject poolObject;
|
||||
private AudioSource audioSource1;
|
||||
private AudioSource audioSource2;
|
||||
private AudioSource currentAudioSource;
|
||||
private Coroutine myCoroutine;
|
||||
|
||||
public MusicPlayer(AudioSourcePool audioSourcePool)
|
||||
public MusicPlayer(IResources resources) : base(resources)
|
||||
{
|
||||
this.audioSourcePool = audioSourcePool;
|
||||
poolObject = GameObject.Find(GetType().Name);
|
||||
if (poolObject == null)
|
||||
{
|
||||
// 如果不存在,创建一个新对象
|
||||
poolObject = new GameObject(GetType().Name);
|
||||
}
|
||||
|
||||
audioSource1 = CreatAudioSource("audioSource1", out AudioSource source1);
|
||||
audioSource2 = CreatAudioSource("audioSource2", out AudioSource source2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -21,15 +31,40 @@ namespace Stary.Evo.AudioCore
|
||||
/// </summary>
|
||||
/// <param name="audioData">{[clip:音频], [volume:音量], [fadeDuration:自然过渡时间],
|
||||
/// [is3DAudio:是否3D音频], [audio3DPosition:3D音频位置], [audio3DMaxDistance:3D音频最大距离]}</param>
|
||||
public override void Play(AudioData audioData)
|
||||
public override async void Play(AudioData audioData)
|
||||
{
|
||||
audioData = AudioDataInitialize(audioData);
|
||||
if (Resources == null)
|
||||
{
|
||||
Debug.LogError("AudioCoreManager: Resources is null, please initialize it first.");
|
||||
return;
|
||||
}
|
||||
|
||||
audioData = Initialize(audioData);
|
||||
|
||||
if (!string.IsNullOrEmpty(audioData.packageName) && !string.IsNullOrEmpty(audioData.packageName))
|
||||
{
|
||||
var clip = await Resources.LoadAssetAsync<AudioClip>(audioData.packageName, audioData.assetName);
|
||||
if (clip == null)
|
||||
{
|
||||
Debug.LogErrorFormat($"从资源包【{audioData.packageName}】加载音频片段【{audioData.assetName}】失败");
|
||||
}
|
||||
else
|
||||
{
|
||||
audioData.clip = clip;
|
||||
}
|
||||
}
|
||||
|
||||
if (audioData.clip == null)
|
||||
{
|
||||
Debug.LogError("播放的音效音频片段为空");
|
||||
return;
|
||||
}
|
||||
|
||||
if (audioSource1 == null)
|
||||
{
|
||||
audioSource1 = audioSourcePool.GetAudioSource("Music");
|
||||
audioSource1.clip = audioData.clip;
|
||||
audioSource1.volume = audioData.volume;
|
||||
|
||||
|
||||
// 设置2D与3D音频
|
||||
if (audioData.is3DAudio)
|
||||
{
|
||||
@@ -53,7 +88,7 @@ namespace Stary.Evo.AudioCore
|
||||
audioSource1.minDistance = 1f;
|
||||
audioSource1.maxDistance = 500f;
|
||||
}
|
||||
|
||||
|
||||
currentAudioSource = audioSource1;
|
||||
currentAudioSource.Play();
|
||||
FadeMusic(audioSource1, audioData.fadeDuration, audioSource2);
|
||||
@@ -62,10 +97,9 @@ namespace Stary.Evo.AudioCore
|
||||
{
|
||||
if (audioSource2 == null)
|
||||
{
|
||||
audioSource2 = audioSourcePool.GetAudioSource("Music");
|
||||
audioSource2.clip = audioData.clip;
|
||||
audioSource2.volume = audioData.volume;
|
||||
|
||||
|
||||
// 设置2D与3D音频
|
||||
if (audioData.is3DAudio)
|
||||
{
|
||||
@@ -89,7 +123,7 @@ namespace Stary.Evo.AudioCore
|
||||
audioSource2.minDistance = 1f;
|
||||
audioSource2.maxDistance = 500f;
|
||||
}
|
||||
|
||||
|
||||
currentAudioSource = audioSource2;
|
||||
currentAudioSource.Play();
|
||||
FadeMusic(audioSource2, audioData.fadeDuration, audioSource1);
|
||||
@@ -99,59 +133,54 @@ namespace Stary.Evo.AudioCore
|
||||
Debug.LogWarning("UnityEvo:已同时存在两个背景乐在切换");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override UniTask PlayAsync(AudioData audioData)
|
||||
{
|
||||
return UniTask.CompletedTask;
|
||||
}
|
||||
|
||||
public override void StopAll()
|
||||
{
|
||||
FadeAllMusic();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭背景音乐
|
||||
/// </summary>
|
||||
/// <param name="audioData">{[fadeDuration:自然过渡时间]}</param>
|
||||
public override void Stop(AudioData audioData)
|
||||
public override void Stop()
|
||||
{
|
||||
FadeAllMusic();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置背景音乐音量
|
||||
/// </summary>
|
||||
/// <param name="audioData"></param>
|
||||
public void SetMusicVolume(AudioData audioData)
|
||||
{
|
||||
if(currentAudioSource == null) return;
|
||||
|
||||
if (myCoroutine != null)
|
||||
{
|
||||
CoroutineHelper.Instance.StopCoroutine(myCoroutine);
|
||||
myCoroutine = null;
|
||||
}
|
||||
myCoroutine = CoroutineHelper.Instance.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)
|
||||
public async UniTask SetMusicVolume(float fadeDuration, float targetVolume)
|
||||
{
|
||||
float startVolume = currentAudioSource.volume;
|
||||
while (currentAudioSource.volume != targetVolume)
|
||||
while (!Mathf.Approximately(currentAudioSource.volume, targetVolume))
|
||||
{
|
||||
if (currentAudioSource.volume > targetVolume)
|
||||
{
|
||||
currentAudioSource.volume -= (startVolume-targetVolume) * Time.deltaTime / fadeDuration;
|
||||
currentAudioSource.volume -= (startVolume - targetVolume) * Time.deltaTime / fadeDuration;
|
||||
}
|
||||
else
|
||||
{
|
||||
currentAudioSource.volume += (targetVolume-startVolume) * Time.deltaTime / fadeDuration;
|
||||
currentAudioSource.volume += (targetVolume - startVolume) * Time.deltaTime / fadeDuration;
|
||||
}
|
||||
|
||||
if (Mathf.Abs(currentAudioSource.volume - targetVolume) < 0.01f)
|
||||
{
|
||||
currentAudioSource.volume = targetVolume;
|
||||
}
|
||||
yield return null;
|
||||
|
||||
await UniTask.Yield();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,78 +193,39 @@ namespace Stary.Evo.AudioCore
|
||||
/// <returns></returns>
|
||||
private void FadeMusic(AudioSource source1, float fadeDuration, AudioSource source2 = null)
|
||||
{
|
||||
CoroutineHelper.Instance.StartCoroutine(FadeInMusic(source1, fadeDuration));
|
||||
|
||||
if (source2 != null)
|
||||
{
|
||||
CoroutineHelper.Instance.StartCoroutine(FadeOutMusic(source2, fadeDuration));
|
||||
}
|
||||
|
||||
FadeInMusic(source1, fadeDuration);
|
||||
FadeOutMusic(source2, fadeDuration);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭音频的协程
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="fadeDuration"></param>
|
||||
/// <returns></returns>
|
||||
private IEnumerator FadeOutMusic(AudioSource source, float fadeDuration)
|
||||
{
|
||||
if (source == null) yield break;
|
||||
float startVolume = source.volume;
|
||||
|
||||
while (source.volume > 0)
|
||||
{
|
||||
source.volume -= startVolume * Time.deltaTime / fadeDuration;
|
||||
yield return null;
|
||||
}
|
||||
|
||||
source.Stop();
|
||||
if (audioSource1 == source)
|
||||
{
|
||||
audioSource1 = null;
|
||||
}
|
||||
else if (audioSource2 == source)
|
||||
{
|
||||
audioSource2 = null;
|
||||
}
|
||||
audioSourcePool.ReturnAudioSource("Music", source.gameObject);
|
||||
}
|
||||
|
||||
private void FadeAllMusic()
|
||||
{
|
||||
if (currentAudioSource != null && currentAudioSource.isPlaying)
|
||||
{
|
||||
if (currentAudioSource == audioSource1)
|
||||
{
|
||||
audioSource1 = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
audioSource2 = null;
|
||||
}
|
||||
audioSourcePool.ReturnAudioSource("Music", currentAudioSource.gameObject);
|
||||
currentAudioSource = null;
|
||||
}
|
||||
|
||||
currentAudioSource = null;
|
||||
|
||||
audioSource1.Stop();
|
||||
audioSource2.Stop();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开启音频的协程
|
||||
/// 创建一个新的AudioSource对象
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="fadeDuration"></param>
|
||||
/// <returns></returns>
|
||||
private IEnumerator FadeInMusic(AudioSource source, float fadeDuration)
|
||||
/// <param name="obj"></param>
|
||||
private AudioSource CreatAudioSource(string name, out AudioSource source)
|
||||
{
|
||||
float targetVolume = source.volume;
|
||||
source.volume = 0;
|
||||
source = new GameObject(name).AddComponent<AudioSource>();
|
||||
source.gameObject.transform.SetParent(poolObject.transform);
|
||||
source.transform.SetParent(poolObject.transform); // 将新对象作为当前对象的子对象
|
||||
source.playOnAwake = false; // 添加 AudioSource 组件并禁用自动播放
|
||||
return source;
|
||||
}
|
||||
|
||||
while (source.volume < targetVolume)
|
||||
{
|
||||
source.volume += targetVolume * Time.deltaTime / fadeDuration;
|
||||
yield return null;
|
||||
}
|
||||
public void Dispose()
|
||||
{
|
||||
FadeAllMusic();
|
||||
Object.Destroy(audioSource1.gameObject);
|
||||
Object.Destroy(audioSource2.gameObject);
|
||||
audioSource1 = null;
|
||||
audioSource2 = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user