2025-03-31 11:49:53 +08:00
|
|
|
|
using UnityEngine;
|
2025-03-06 17:24:31 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
|
|
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 AudioSourcePool
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
private Dictionary<string, Queue<GameObject>> poolDict = new Dictionary<string, Queue<GameObject>>();
|
|
|
|
|
|
private GameObject poolObject;
|
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>
|
|
|
|
|
|
private void PoolAwake()
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-31 11:49:53 +08:00
|
|
|
|
// 检查是否已经存在一个名为"AudioSourcePool"的对象
|
2025-03-26 09:34:52 +08:00
|
|
|
|
SceneManager.sceneUnloaded += OnSceneUnloaded;
|
|
|
|
|
|
poolObject = GameObject.Find("AudioSourcePool");
|
|
|
|
|
|
if (poolObject == null)
|
|
|
|
|
|
{
|
2025-03-31 11:49:53 +08:00
|
|
|
|
// 如果不存在,创建一个新对象
|
2025-03-26 09:34:52 +08:00
|
|
|
|
poolObject = new GameObject("AudioSourcePool");
|
|
|
|
|
|
}
|
|
|
|
|
|
CoroutineHelper.SetRunner();
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-31 11:49:53 +08:00
|
|
|
|
// 初始化 Voice 池(最多 1 个,可动态扩展)
|
2025-03-26 09:34:52 +08:00
|
|
|
|
poolDict["Voice"] = new Queue<GameObject>();
|
|
|
|
|
|
CreateAudioSource("Voice");
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-31 11:49:53 +08:00
|
|
|
|
// 初始化 Music 池(最多 2 个)
|
2025-03-26 09:34:52 +08:00
|
|
|
|
poolDict["Music"] = new Queue<GameObject>();
|
|
|
|
|
|
for (int i = 0; i < 2; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateAudioSource("Music");
|
|
|
|
|
|
}
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-31 11:49:53 +08:00
|
|
|
|
// 初始化 SFX 池(初始 4 个,可动态扩展)
|
2025-03-26 09:34:52 +08:00
|
|
|
|
poolDict["SFX"] = new Queue<GameObject>();
|
|
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateAudioSource("SFX");
|
|
|
|
|
|
}
|
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="type">定义对象类型</param>
|
2025-03-26 09:34:52 +08:00
|
|
|
|
private void CreateAudioSource(string type)
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
GameObject newObject = new GameObject($"AudioSource_{type}");
|
2025-03-31 11:49:53 +08:00
|
|
|
|
newObject.transform.SetParent(poolObject.transform); // 将新对象作为当前对象的子对象
|
|
|
|
|
|
newObject.AddComponent<AudioSource>().playOnAwake = false; // 添加 AudioSource 组件并禁用自动播放
|
2025-03-26 09:34:52 +08:00
|
|
|
|
if (type == "Music")
|
|
|
|
|
|
{
|
|
|
|
|
|
newObject.GetComponent<AudioSource>().loop = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
poolDict[type].Enqueue(newObject);
|
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="type"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public AudioSource GetAudioSource(string type)
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
if (poolObject == null)
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
PoolAwake();
|
2025-03-06 17:24:31 +08:00
|
|
|
|
}
|
2025-03-26 09:34:52 +08:00
|
|
|
|
|
|
|
|
|
|
if (!poolDict.ContainsKey(type))
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-31 11:49:53 +08:00
|
|
|
|
Debug.LogError($"对象池中不存在类型: {type}");
|
2025-03-06 17:24:31 +08:00
|
|
|
|
return null;
|
2025-03-07 15:21:24 +08:00
|
|
|
|
}
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
if (poolDict[type].Count == 0)
|
|
|
|
|
|
{
|
2025-03-31 11:49:53 +08:00
|
|
|
|
// 如果池为空,动态创建新的 GameObject(仅限 SFX 与 Voice)
|
2025-03-26 09:34:52 +08:00
|
|
|
|
if (type == "SFX" && type == "Voice")
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateAudioSource(type);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2025-03-31 11:49:53 +08:00
|
|
|
|
Debug.LogWarning($"对象池 {type} 已用完,无法分配新的 AudioSource");
|
2025-03-26 09:34:52 +08:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
CreateAudioSource(type);
|
|
|
|
|
|
}
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
GameObject audioObject = poolDict[type].Dequeue();
|
|
|
|
|
|
AudioSource audioSource = audioObject.GetComponent<AudioSource>();
|
|
|
|
|
|
return audioSource;
|
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="type"></param>
|
|
|
|
|
|
/// <param name="audioObject"></param>
|
|
|
|
|
|
public void ReturnAudioSource(string type, GameObject audioObject)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!poolDict.ContainsKey(type))
|
|
|
|
|
|
{
|
2025-03-31 11:49:53 +08:00
|
|
|
|
Debug.LogError($"对象池中不存在类型: {type}");
|
2025-03-26 09:34:52 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2025-03-06 17:24:31 +08:00
|
|
|
|
|
2025-03-26 09:34:52 +08:00
|
|
|
|
AudioSource audioSource = audioObject.GetComponent<AudioSource>();
|
2025-03-31 11:49:53 +08:00
|
|
|
|
audioSource.Stop(); // 停止播放
|
|
|
|
|
|
audioSource.clip = null; // 清空音频剪辑
|
|
|
|
|
|
audioSource.volume = 1f; // 音量大小恢复
|
|
|
|
|
|
poolDict[type].Enqueue(audioObject); // 回收到对象池
|
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="scene"></param>
|
|
|
|
|
|
void OnSceneUnloaded(Scene scene)
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
foreach (var pair in poolDict)
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
Queue<GameObject> queue = pair.Value;
|
|
|
|
|
|
while (queue.Count > 0)
|
2025-03-06 17:24:31 +08:00
|
|
|
|
{
|
2025-03-26 09:34:52 +08:00
|
|
|
|
GameObject obj = queue.Dequeue();
|
|
|
|
|
|
if (obj != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
UnityEngine.Object.Destroy(obj);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-03-06 17:24:31 +08:00
|
|
|
|
}
|
2025-03-26 09:34:52 +08:00
|
|
|
|
poolDict.Clear();
|
2025-03-06 17:24:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|