【m】修复背景音乐不播的问题

This commit is contained in:
mzh
2026-01-09 17:56:38 +08:00
parent f5a79aabf8
commit d85172aa7e
2 changed files with 31 additions and 35 deletions

View File

@@ -22,8 +22,8 @@ namespace Stary.Evo.AudioCore
poolObject = new GameObject(GetType().Name);
}
audioSource1 = CreatAudioSource("audioSource1", out AudioSource source1);
audioSource2 = CreatAudioSource("audioSource2", out AudioSource source2);
CreatAudioSource("audioSource1", out audioSource1);
CreatAudioSource("audioSource2", out audioSource2);
}
/// <summary>
@@ -41,7 +41,7 @@ namespace Stary.Evo.AudioCore
audioData = Initialize(audioData);
if (!string.IsNullOrEmpty(audioData.packageName) && !string.IsNullOrEmpty(audioData.packageName))
if (!string.IsNullOrEmpty(audioData.packageName) && !string.IsNullOrEmpty(audioData.assetName))
{
var clip = await Resources.LoadAssetAsync<AudioClip>(audioData.packageName, audioData.assetName);
if (clip == null)
@@ -60,7 +60,7 @@ namespace Stary.Evo.AudioCore
return;
}
if (audioSource1 == null)
if (audioSource1 != null && audioSource1.isPlaying == false)
{
audioSource1.clip = audioData.clip;
audioSource1.volume = audioData.volume;
@@ -93,45 +93,42 @@ namespace Stary.Evo.AudioCore
currentAudioSource.Play();
FadeMusic(audioSource1, audioData.fadeDuration, audioSource2);
}
else
else if (audioSource2 != null && audioSource2.isPlaying == false)
{
if (audioSource2 == null)
{
audioSource2.clip = audioData.clip;
audioSource2.volume = audioData.volume;
audioSource2.clip = audioData.clip;
audioSource2.volume = audioData.volume;
// 设置2D与3D音频
if (audioData.is3DAudio)
// 设置2D与3D音频
if (audioData.is3DAudio)
{
audioSource2.transform.position = audioData.audio3DPosition;
audioSource2.spatialBlend = 1;
audioSource2.minDistance = 1f;
if (audioData.audio3DMaxDistance != 0)
{
audioSource2.transform.position = audioData.audio3DPosition;
audioSource2.spatialBlend = 1;
audioSource2.minDistance = 1f;
if (audioData.audio3DMaxDistance != 0)
{
audioSource2.maxDistance = audioData.audio3DMaxDistance;
}
else
{
// 默认3D最大距离为3米
audioSource2.maxDistance = 3f;
}
audioSource2.maxDistance = audioData.audio3DMaxDistance;
}
else
{
audioSource2.transform.position = Vector3.zero;
audioSource2.spatialBlend = 0;
audioSource2.minDistance = 1f;
audioSource2.maxDistance = 500f;
// 默认3D最大距离为3米
audioSource2.maxDistance = 3f;
}
currentAudioSource = audioSource2;
currentAudioSource.Play();
FadeMusic(audioSource2, audioData.fadeDuration, audioSource1);
}
else
{
Debug.LogWarning("UnityEvo:已同时存在两个背景乐在切换");
audioSource2.transform.position = Vector3.zero;
audioSource2.spatialBlend = 0;
audioSource2.minDistance = 1f;
audioSource2.maxDistance = 500f;
}
currentAudioSource = audioSource2;
currentAudioSource.Play();
FadeMusic(audioSource2, audioData.fadeDuration, audioSource1);
}
else
{
Debug.LogWarning("UnityEvo:已同时存在两个背景乐在切换");
}
}
@@ -215,13 +212,12 @@ namespace Stary.Evo.AudioCore
/// 创建一个新的AudioSource对象
/// </summary>
/// <param name="obj"></param>
private AudioSource CreatAudioSource(string name, out AudioSource source)
private void CreatAudioSource(string name, out AudioSource source)
{
source = new GameObject(name).AddComponent<AudioSource>();
source.gameObject.transform.SetParent(poolObject.transform);
source.transform.SetParent(poolObject.transform); // 将新对象作为当前对象的子对象
source.playOnAwake = false; // 添加 AudioSource 组件并禁用自动播放
return source;
}
public void Dispose()