Files

288 lines
9.0 KiB
C#
Raw Permalink Normal View History

2025-12-19 11:55:13 +08:00
using System;
using UnityEngine;
2025-03-06 17:24:31 +08:00
using System.Collections.Generic;
2025-12-31 15:56:05 +08:00
using System.Threading;
2025-03-06 17:24:31 +08:00
using UnityEngine.SceneManagement;
2025-03-31 11:49:53 +08:00
namespace Stary.Evo.AudioCore
2025-03-06 17:24:31 +08:00
{
2025-12-31 15:56:05 +08:00
/// <summary>
/// 对象池
/// </summary>
public interface IAudioSourcePool
{
/// <summary>
/// 将对象存进池里
/// </summary>
/// <param name="obj"></param>
void Creation();
/// <summary>
/// 从未激活池里面取对象,并放入激活池
/// </summary>
/// <returns></returns>
AudioSourceToken Spawn();
/// <summary>
/// 从激活池中释放对象到未激活池中
/// </summary>
AudioSourceToken RecycleSpawn(AudioSourceToken source);
/// <summary>
/// 回收对象
/// </summary>
void RecycleAll();
/// <summary>
/// 清空对象池
/// </summary>
void RemoveAll();
int GetPolLength();
}
public class AudioSourcePool : IAudioSourcePool
2025-03-06 17:24:31 +08:00
{
2025-12-31 18:10:58 +08:00
private Transform active;
2025-03-06 17:24:31 +08:00
2025-03-26 09:34:52 +08:00
/// <summary>
2025-12-31 15:56:05 +08:00
/// 回收对象的父物体
2025-03-26 09:34:52 +08:00
/// </summary>
2025-12-31 15:56:05 +08:00
private Transform recycle;
protected List<AudioSourceToken> activepool = new List<AudioSourceToken>();
protected Queue<AudioSourceToken> inactivepool = new Queue<AudioSourceToken>();
//没有回收的个数
protected int noRecycleCount;
/// <summary>
/// 对象池物体总数量
/// </summary>
public int poolCount = 0;
/// <summary>
/// 对象池最大数量
/// </summary>
private int poolSize = 0;
public AudioSourcePool(string poolName, int poolSize)
2025-03-06 17:24:31 +08:00
{
2025-12-31 15:56:05 +08:00
this.poolSize = poolSize;
2025-12-31 18:10:58 +08:00
var poolObject = GameObject.Find(poolName);
2025-03-26 09:34:52 +08:00
if (poolObject == null)
{
2025-03-31 11:49:53 +08:00
// 如果不存在,创建一个新对象
2025-12-31 18:10:58 +08:00
active = new GameObject(poolName).transform;
}
else
{
active = poolObject.transform;
2025-03-26 09:34:52 +08:00
}
2025-03-06 17:24:31 +08:00
2025-12-31 18:10:58 +08:00
var recycleObject = GameObject.Find($"recycle_{poolName}");
if (recycle == null)
2025-03-26 09:34:52 +08:00
{
2025-12-31 15:56:05 +08:00
// 如果不存在,创建一个新对象
2025-12-31 18:10:58 +08:00
recycle = new GameObject($"recycle_{poolName}").transform;
}
else
{
recycle = recycleObject.transform;
2025-03-26 09:34:52 +08:00
}
2025-03-06 17:24:31 +08:00
}
2025-03-26 09:34:52 +08:00
/// <summary>
2025-12-31 15:56:05 +08:00
/// 将对象存进池里
2025-03-26 09:34:52 +08:00
/// </summary>
2025-12-31 15:56:05 +08:00
/// <param name="obj"></param>
public void Creation()
2025-03-06 17:24:31 +08:00
{
2025-12-31 15:56:05 +08:00
AudioSource source = CreatAudioSource();
if (source == null)
2025-03-26 09:34:52 +08:00
{
2025-12-31 18:10:58 +08:00
Debug.LogErrorFormat("对象池【{0}】已达最大数量【{1}】,无法创建新对象", active.name, poolSize);
2025-12-31 15:56:05 +08:00
return;
2025-03-26 09:34:52 +08:00
}
2025-12-31 15:56:05 +08:00
// if (type == "Music")
// {
// newObject.GetComponent<AudioSource>().loop = true;
// }
inactivepool.Enqueue(new AudioSourceToken(source, new CancellationTokenSource()));
noRecycleCount++;
2025-03-06 17:24:31 +08:00
}
2025-12-31 15:56:05 +08:00
2025-03-26 09:34:52 +08:00
/// <summary>
2025-12-31 15:56:05 +08:00
/// 从未激活池里面取对象,并放入激活池
2025-03-26 09:34:52 +08:00
/// </summary>
2025-12-31 15:56:05 +08:00
/// <param name="createIfPoolEmpty">如果为空是否new出来</param>
public AudioSourceToken Spawn()
2025-03-06 17:24:31 +08:00
{
2025-12-31 15:56:05 +08:00
AudioSourceToken audioSourceToken = null;
if (noRecycleCount > 0)
2025-03-06 17:24:31 +08:00
{
2025-12-31 15:56:05 +08:00
audioSourceToken = inactivepool.Dequeue();
2025-12-31 18:10:58 +08:00
audioSourceToken.source.transform.parent = active;
2025-12-31 15:56:05 +08:00
noRecycleCount--;
if (audioSourceToken.source == null)
Debug.LogErrorFormat("对象池中不存在此对象【{0}】请排查代码", audioSourceToken.source);
audioSourceToken.cancellationToken = new CancellationTokenSource();
2025-03-06 17:24:31 +08:00
}
2025-12-31 15:56:05 +08:00
else
2025-03-06 17:24:31 +08:00
{
2025-12-31 15:56:05 +08:00
var source = CreatAudioSource();
if (source == null)
2025-03-26 09:34:52 +08:00
{
2025-12-31 18:10:58 +08:00
Debug.LogErrorFormat("对象池【{0}】已达最大数量【{1}】,无法创建新对象", active.name, poolSize);
2025-03-26 09:34:52 +08:00
return null;
}
2025-03-06 17:24:31 +08:00
2025-12-31 15:56:05 +08:00
audioSourceToken = new AudioSourceToken(source, new CancellationTokenSource());
2025-03-26 09:34:52 +08:00
}
2025-03-06 17:24:31 +08:00
2025-12-31 15:56:05 +08:00
audioSourceToken.source.transform.localPosition = Vector3.zero;
audioSourceToken.source.transform.localRotation = Quaternion.identity;
audioSourceToken.source.transform.localScale = Vector3.one;
activepool.Add(audioSourceToken);
return audioSourceToken;
2025-03-06 17:24:31 +08:00
}
2025-03-26 09:34:52 +08:00
/// <summary>
2025-12-31 15:56:05 +08:00
/// 从激活池中释放对象到未激活池中
2025-03-26 09:34:52 +08:00
/// </summary>
2025-12-31 15:56:05 +08:00
public AudioSourceToken RecycleSpawn(AudioSourceToken audioSourceToken)
2025-03-26 09:34:52 +08:00
{
2025-12-31 15:56:05 +08:00
if (audioSourceToken.source != null && activepool.Contains(audioSourceToken))
2025-03-26 09:34:52 +08:00
{
2025-12-31 15:56:05 +08:00
activepool.Remove(audioSourceToken);
inactivepool.Enqueue(audioSourceToken);
audioSourceToken.source.transform.parent = recycle;
audioSourceToken.source.Stop(); // 停止播放
audioSourceToken.source.clip = null; // 清空音频剪辑
audioSourceToken.source.volume = 1f; // 音量大小恢复
noRecycleCount++;
// 释放CancellationToken资源
audioSourceToken.Dispose();
2025-03-26 09:34:52 +08:00
}
2025-03-06 17:24:31 +08:00
2025-12-31 15:56:05 +08:00
return null;
2025-03-26 09:34:52 +08:00
}
/// <summary>
2025-12-31 15:56:05 +08:00
/// 回收所有对象
2025-03-26 09:34:52 +08:00
/// </summary>
2025-12-31 15:56:05 +08:00
public void RecycleAll()
2025-03-06 17:24:31 +08:00
{
2025-12-31 15:56:05 +08:00
for (int i = 0; i < activepool.Count; i++)
2025-03-06 17:24:31 +08:00
{
2025-12-31 15:56:05 +08:00
RecycleSpawn(activepool[i]);
2025-03-06 17:24:31 +08:00
}
}
2025-12-31 15:56:05 +08:00
/// <summary>
/// 清空对象池
/// </summary>
public void RemoveAll()
{
noRecycleCount = 0;
poolCount = 0;
2025-12-31 18:10:58 +08:00
GameObject.Destroy(active.gameObject);
2025-12-31 15:56:05 +08:00
inactivepool.Clear();
GameObject.Destroy(recycle.gameObject);
for (int i = 0; i < activepool.Count; i++)
{
RecycleSpawn(activepool[i]);
}
activepool.Clear();
}
/// <summary>
/// 创建一个新的AudioSource对象
/// </summary>
/// <param name="obj"></param>
private AudioSource CreatAudioSource()
{
if (poolCount >= poolSize && poolSize > 0)
return null;
poolCount++;
2025-12-31 18:10:58 +08:00
AudioSource source = new GameObject($"{active.name}_{poolCount}").AddComponent<AudioSource>();
2025-12-31 15:56:05 +08:00
source.gameObject.transform.SetParent(recycle);
2025-12-31 18:10:58 +08:00
source.transform.SetParent(active.transform); // 将新对象作为当前对象的子对象
2025-12-31 15:56:05 +08:00
source.playOnAwake = false; // 添加 AudioSource 组件并禁用自动播放
return source;
}
public int GetPolLength()
{
return inactivepool.Count;
}
}
public class AudioSourceToken
{
public AudioSource source;
public CancellationTokenSource cancellationToken;
public AudioSourceToken(AudioSource source, CancellationTokenSource cancellationToken)
{
this.source = source;
this.cancellationToken = cancellationToken;
}
2025-12-31 18:10:58 +08:00
public AudioSourceToken(AudioSourceToken sourceToken)
{
this.source = sourceToken.source;
this.cancellationToken = sourceToken.cancellationToken;
}
2025-12-31 15:56:05 +08:00
// 添加释放方法
2025-12-19 11:55:13 +08:00
public void Dispose()
{
2025-12-31 15:56:05 +08:00
// 使用局部变量避免多线程环境下的竞态条件
var cts = Interlocked.Exchange(ref cancellationToken, null);
if (cts != null)
2025-12-19 11:55:13 +08:00
{
2025-12-31 15:56:05 +08:00
try
{
// 取消令牌,触发注册的回调
cts.Cancel();
cts.Dispose();
}
catch (ObjectDisposedException)
{
// 已经被释放,无需处理
}
catch (AggregateException ex)
{
// 处理注册回调抛出的异常
// 只记录第一个异常,避免日志过于冗长
Debug.LogError($"CancellationTokenSource.Cancel() 回调抛出异常: {ex.InnerExceptions[0]}");
// 可以选择不重新抛出,避免中断释放流程
}
catch (Exception ex)
{
Debug.LogError($"CancellationTokenSource.Cancel() 抛出异常: {ex}");
}
finally
{
// 确保始终释放资源
try
{
cts.Dispose();
}
catch (ObjectDisposedException)
{
// 已经被释放,无需处理
}
}
2025-12-19 11:55:13 +08:00
}
}
2025-03-06 17:24:31 +08:00
}
}