Files
plugin-library/Assets/04.AudioCore/RunTime/Base/SFXPlayer.cs

103 lines
3.5 KiB
C#
Raw Normal View History

2025-03-31 11:49:53 +08:00
using System.Collections;
2025-03-06 17:24:31 +08:00
using System.Collections.Generic;
using UnityEngine;
2025-03-31 11:49:53 +08:00
namespace Stary.Evo.AudioCore
2025-03-06 17:24:31 +08:00
{
2025-03-26 09:34:52 +08:00
public class SFXPlayer : AbstractAudio
2025-03-06 17:24:31 +08:00
{
2025-03-26 09:34:52 +08:00
private AudioSourcePool audioSourcePool;
2025-03-31 11:49:53 +08:00
private List<AudioSource> activeSources = new List<AudioSource>(); // 正在播放的 AudioSource 列表
2025-03-06 17:24:31 +08:00
2025-03-26 09:34:52 +08:00
public SFXPlayer(AudioSourcePool audioSourcePool)
{
this.audioSourcePool = audioSourcePool;
}
2025-03-06 17:24:31 +08:00
2025-03-26 09:34:52 +08:00
/// <summary>
2025-03-31 11:49:53 +08:00
/// 播放音效
2025-03-26 09:34:52 +08:00
/// </summary>
2025-03-31 11:49:53 +08:00
/// <param name="audioData">{[clip:音频], [volume:音量],
2025-07-16 13:54:05 +08:00
/// [onComplete:回调行为], [delayOnCompleteTime:延迟回调执行的时间],
/// [is3DAudio:是否3D音频], [audio3DPosition:3D音频位置], [audio3DMaxDistance:3D音频最大距离]}</param>
2025-03-26 09:34:52 +08:00
public override void Play(AudioData audioData)
{
audioData = AudioDataInitialize(audioData);
if (audioData.clip == null)
{
Debug.LogError("播放的音效音频片段为空");
return;
}
2025-03-26 09:34:52 +08:00
AudioSource source = audioSourcePool.GetAudioSource("SFX");
if (source == null) return;
2025-03-06 17:24:31 +08:00
2025-03-26 09:34:52 +08:00
source.clip = audioData.clip;
source.volume = audioData.volume;
2025-07-16 13:54:05 +08:00
// 设置2D与3D音频
if (audioData.is3DAudio)
{
source.transform.position = audioData.audio3DPosition;
source.spatialBlend = 1;
source.minDistance = 1f;
if (audioData.audio3DMaxDistance != 0)
{
source.maxDistance = audioData.audio3DMaxDistance;
}
else
{
// 默认3D最大距离为3米
source.maxDistance = 3f;
}
}
else
{
source.transform.position = Vector3.zero;
source.spatialBlend = 0;
source.minDistance = 1f;
source.maxDistance = 500f;
}
2025-03-26 09:34:52 +08:00
source.Play();
2025-03-06 17:24:31 +08:00
2025-03-31 11:49:53 +08:00
// 将 AudioSource 加入活动列表
2025-03-26 09:34:52 +08:00
activeSources.Add(source);
2025-03-06 17:24:31 +08:00
2025-03-31 11:49:53 +08:00
// 使用协程处理延迟和回调
2025-12-19 11:55:13 +08:00
CoroutineHelper.Instance.StartCoroutine(PlaySFXCoroutine(source, audioData.delayOnCompleteTime, audioData.onComplete));
2025-03-26 09:34:52 +08:00
}
/// <summary>
2025-03-31 11:49:53 +08:00
/// 停止所有音效
2025-03-26 09:34:52 +08:00
/// </summary>
2025-03-31 11:49:53 +08:00
/// <param name="audioData">{[无可使用变量]}</param>
2025-06-04 14:57:54 +08:00
public override void Stop()
2025-03-06 17:24:31 +08:00
{
if (activeSources == null) return;
2025-03-26 09:34:52 +08:00
foreach (var source in activeSources)
2025-03-06 17:24:31 +08:00
{
2025-03-26 09:34:52 +08:00
if (source.isPlaying)
{
source.Stop();
audioSourcePool.ReturnAudioSource("SFX", source.gameObject);
}
2025-03-06 17:24:31 +08:00
}
2025-03-26 09:34:52 +08:00
activeSources.Clear();
2025-03-06 17:24:31 +08:00
}
2025-03-26 09:34:52 +08:00
/// <summary>
2025-03-31 11:49:53 +08:00
/// 播放音效的协程
2025-03-26 09:34:52 +08:00
/// </summary>
/// <param name="source"></param>
/// <param name="delay"></param>
/// <param name="onComplete"></param>
/// <returns></returns>
private IEnumerator PlaySFXCoroutine(AudioSource source, float delay, System.Action onComplete)
{
yield return new WaitForSeconds(source.clip.length + delay);
audioSourcePool.ReturnAudioSource("SFX", source.gameObject);
onComplete?.Invoke();
2025-03-26 09:34:52 +08:00
activeSources.Remove(source);
}
2025-03-06 17:24:31 +08:00
}
}