diff --git a/Assets/04.AudioCore/RunTime/Base/MusicPlayer.cs b/Assets/04.AudioCore/RunTime/Base/MusicPlayer.cs index a899ac6..c7a722e 100644 --- a/Assets/04.AudioCore/RunTime/Base/MusicPlayer.cs +++ b/Assets/04.AudioCore/RunTime/Base/MusicPlayer.cs @@ -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); } /// @@ -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(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对象 /// /// - private AudioSource CreatAudioSource(string name, out AudioSource source) + private void CreatAudioSource(string name, out AudioSource source) { source = new GameObject(name).AddComponent(); source.gameObject.transform.SetParent(poolObject.transform); source.transform.SetParent(poolObject.transform); // 将新对象作为当前对象的子对象 source.playOnAwake = false; // 添加 AudioSource 组件并禁用自动播放 - return source; } public void Dispose() diff --git a/Assets/04.AudioCore/package.json b/Assets/04.AudioCore/package.json index a19bfa4..c457d55 100644 --- a/Assets/04.AudioCore/package.json +++ b/Assets/04.AudioCore/package.json @@ -1,6 +1,6 @@ { "name": "com.staryevo.audiocore", - "version": "1.1.1", + "version": "1.1.2", "displayName": "04.AudioCore", "description": "音频播放工具", "unity": "2021.3",