【m】 修改淡入淡出

This commit is contained in:
zhangzheng
2025-12-31 18:10:58 +08:00
parent 189c5016be
commit 2d23b1e12f
7 changed files with 91 additions and 28 deletions

View File

@@ -43,7 +43,7 @@ namespace Stary.Evo.AudioCore
public class AudioSourcePool : IAudioSourcePool
{
private GameObject poolObject;
private Transform active;
/// <summary>
/// 回收对象的父物体
@@ -70,18 +70,26 @@ namespace Stary.Evo.AudioCore
public AudioSourcePool(string poolName, int poolSize)
{
this.poolSize = poolSize;
poolObject = GameObject.Find(poolName);
var poolObject = GameObject.Find(poolName);
if (poolObject == null)
{
// 如果不存在,创建一个新对象
poolObject = new GameObject(poolName);
active = new GameObject(poolName).transform;
}
else
{
active = poolObject.transform;
}
poolObject = GameObject.Find($"recycle_{poolName}");
if (poolObject == null)
var recycleObject = GameObject.Find($"recycle_{poolName}");
if (recycle == null)
{
// 如果不存在,创建一个新对象
poolObject = new GameObject($"recycle_{poolName}");
recycle = new GameObject($"recycle_{poolName}").transform;
}
else
{
recycle = recycleObject.transform;
}
}
@@ -94,7 +102,7 @@ namespace Stary.Evo.AudioCore
AudioSource source = CreatAudioSource();
if (source == null)
{
Debug.LogErrorFormat("对象池【{0}】已达最大数量【{1}】,无法创建新对象", poolObject.name, poolSize);
Debug.LogErrorFormat("对象池【{0}】已达最大数量【{1}】,无法创建新对象", active.name, poolSize);
return;
}
@@ -118,7 +126,7 @@ namespace Stary.Evo.AudioCore
if (noRecycleCount > 0)
{
audioSourceToken = inactivepool.Dequeue();
audioSourceToken.source.transform.SetParent(poolObject.gameObject.transform);
audioSourceToken.source.transform.parent = active;
noRecycleCount--;
if (audioSourceToken.source == null)
Debug.LogErrorFormat("对象池中不存在此对象【{0}】请排查代码", audioSourceToken.source);
@@ -129,7 +137,7 @@ namespace Stary.Evo.AudioCore
var source = CreatAudioSource();
if (source == null)
{
Debug.LogErrorFormat("对象池【{0}】已达最大数量【{1}】,无法创建新对象", poolObject.name, poolSize);
Debug.LogErrorFormat("对象池【{0}】已达最大数量【{1}】,无法创建新对象", active.name, poolSize);
return null;
}
@@ -184,7 +192,7 @@ namespace Stary.Evo.AudioCore
{
noRecycleCount = 0;
poolCount = 0;
GameObject.Destroy(poolObject);
GameObject.Destroy(active.gameObject);
inactivepool.Clear();
GameObject.Destroy(recycle.gameObject);
for (int i = 0; i < activepool.Count; i++)
@@ -204,9 +212,9 @@ namespace Stary.Evo.AudioCore
if (poolCount >= poolSize && poolSize > 0)
return null;
poolCount++;
AudioSource source = new GameObject($"{poolObject.name}_{poolCount}").AddComponent<AudioSource>();
AudioSource source = new GameObject($"{active.name}_{poolCount}").AddComponent<AudioSource>();
source.gameObject.transform.SetParent(recycle);
source.transform.SetParent(poolObject.transform); // 将新对象作为当前对象的子对象
source.transform.SetParent(active.transform); // 将新对象作为当前对象的子对象
source.playOnAwake = false; // 添加 AudioSource 组件并禁用自动播放
return source;
}
@@ -228,6 +236,12 @@ namespace Stary.Evo.AudioCore
this.cancellationToken = cancellationToken;
}
public AudioSourceToken(AudioSourceToken sourceToken)
{
this.source = sourceToken.source;
this.cancellationToken = sourceToken.cancellationToken;
}
// 添加释放方法
public void Dispose()
{

View File

@@ -145,6 +145,11 @@ namespace Stary.Evo.AudioCore
FadeAllMusic();
}
public override AudioSource GetAudioSource()
{
return currentAudioSource;
}
/// <summary>
/// 关闭背景音乐
/// </summary>

View File

@@ -22,8 +22,7 @@ namespace Stary.Evo.AudioCore
{
AudioSourceToken audioSourceToken = await PlayAudio(audioData);
// 使用协程处理延迟和回调
PlayAudioAWait(audioSourceToken, audioData.delayOnCompleteTime,
audioData.onComplete);
PlayAudioAWait(audioSourceToken, audioData.delayOnCompleteTime, audioData.onComplete);
}
public override async UniTask PlayAsync(AudioData audioData)
@@ -113,6 +112,12 @@ namespace Stary.Evo.AudioCore
AudioSourcePool.RecycleAll();
}
public override AudioSource GetAudioSource()
{
Debug.LogWarning("SFXPlayer: GetAudioSource is not implemented.");
return null;
}
public void Dispose()
{
AudioSourcePool.RecycleAll();

View File

@@ -27,7 +27,7 @@ namespace Stary.Evo.AudioCore
AudioSourceToken audioSourceToken = await PlayAudio(audioData);
// 使用协程处理延迟和回调
PlayAudioAWait(audioSourceToken,
audioData.delayOnCompleteTime, audioData.onComplete);
audioData.delayOnCompleteTime, audioData.onComplete, audioData.fadeDuration);
}
/// <summary>
@@ -41,7 +41,7 @@ namespace Stary.Evo.AudioCore
public override async UniTask PlayAsync(AudioData audioData)
{
AudioSourceToken audioSourceToken = await PlayAudio(audioData);
await PlayAudioAWait(audioSourceToken, audioData.delayOnCompleteTime);
await PlayAudioAWait(audioSourceToken, audioData.delayOnCompleteTime, audioData.fadeDuration);
}
@@ -113,7 +113,7 @@ namespace Stary.Evo.AudioCore
}
currentSource.source.Play();
FadeInMusic(currentSource.source, audioData.fadeDuration);
return audioSourceToken;
}
@@ -137,6 +137,11 @@ namespace Stary.Evo.AudioCore
AudioSourcePool.RecycleAll();
}
public override AudioSource GetAudioSource()
{
return currentSource.source;
}
public void Dispose()
{
AudioSourcePool.RecycleAll();