AudioCore0.11

This commit is contained in:
Han
2025-03-07 15:21:24 +08:00
parent a594b6506d
commit 186a9da66c
8 changed files with 194 additions and 74 deletions

View File

@@ -4,7 +4,6 @@ using UnityEngine.SceneManagement;
public class AudioSourcePool
{
// 对象池字典,存储类型和对应的 GameObject 队列
private Dictionary<string, Queue<GameObject>> poolDict = new Dictionary<string, Queue<GameObject>>();
private GameObject poolObject;
@@ -23,7 +22,7 @@ public class AudioSourcePool
}
CoroutineHelper.SetRunner();
// 初始化 Voice 池(最多 1 个)
// 初始化 Voice 池(最多 1 个,可动态扩展
poolDict["Voice"] = new Queue<GameObject>();
CreateAudioSource("Voice");
@@ -51,6 +50,10 @@ public class AudioSourcePool
GameObject newObject = new GameObject($"AudioSource_{type}");
newObject.transform.SetParent(poolObject.transform); // 将新对象作为当前对象的子对象
newObject.AddComponent<AudioSource>().playOnAwake = false; // 添加 AudioSource 组件并禁用自动播放
if(type == "Music")
{
newObject.GetComponent<AudioSource>().loop = true;
}
poolDict[type].Enqueue(newObject);
}
@@ -74,8 +77,8 @@ public class AudioSourcePool
if (poolDict[type].Count == 0)
{
/*// 如果池为空,动态创建新的 GameObject仅限 SFX
if (type == "SFX")
// 如果池为空,动态创建新的 GameObject仅限 SFX 与 Voice
if (type == "SFX" && type == "Voice")
{
CreateAudioSource(type);
}
@@ -83,7 +86,7 @@ public class AudioSourcePool
{
Debug.LogWarning($"对象池 {type} 已用完,无法分配新的 AudioSource");
return null;
}*/
}
CreateAudioSource(type);
}