【m】修复背景音乐不播的问题
This commit is contained in:
@@ -22,8 +22,8 @@ namespace Stary.Evo.AudioCore
|
|||||||
poolObject = new GameObject(GetType().Name);
|
poolObject = new GameObject(GetType().Name);
|
||||||
}
|
}
|
||||||
|
|
||||||
audioSource1 = CreatAudioSource("audioSource1", out AudioSource source1);
|
CreatAudioSource("audioSource1", out audioSource1);
|
||||||
audioSource2 = CreatAudioSource("audioSource2", out AudioSource source2);
|
CreatAudioSource("audioSource2", out audioSource2);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -41,7 +41,7 @@ namespace Stary.Evo.AudioCore
|
|||||||
|
|
||||||
audioData = Initialize(audioData);
|
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);
|
var clip = await Resources.LoadAssetAsync<AudioClip>(audioData.packageName, audioData.assetName);
|
||||||
if (clip == null)
|
if (clip == null)
|
||||||
@@ -60,7 +60,7 @@ namespace Stary.Evo.AudioCore
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (audioSource1 == null)
|
if (audioSource1 != null && audioSource1.isPlaying == false)
|
||||||
{
|
{
|
||||||
audioSource1.clip = audioData.clip;
|
audioSource1.clip = audioData.clip;
|
||||||
audioSource1.volume = audioData.volume;
|
audioSource1.volume = audioData.volume;
|
||||||
@@ -93,45 +93,42 @@ namespace Stary.Evo.AudioCore
|
|||||||
currentAudioSource.Play();
|
currentAudioSource.Play();
|
||||||
FadeMusic(audioSource1, audioData.fadeDuration, audioSource2);
|
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音频
|
// 设置2D与3D音频
|
||||||
if (audioData.is3DAudio)
|
if (audioData.is3DAudio)
|
||||||
|
{
|
||||||
|
audioSource2.transform.position = audioData.audio3DPosition;
|
||||||
|
audioSource2.spatialBlend = 1;
|
||||||
|
audioSource2.minDistance = 1f;
|
||||||
|
if (audioData.audio3DMaxDistance != 0)
|
||||||
{
|
{
|
||||||
audioSource2.transform.position = audioData.audio3DPosition;
|
audioSource2.maxDistance = audioData.audio3DMaxDistance;
|
||||||
audioSource2.spatialBlend = 1;
|
|
||||||
audioSource2.minDistance = 1f;
|
|
||||||
if (audioData.audio3DMaxDistance != 0)
|
|
||||||
{
|
|
||||||
audioSource2.maxDistance = audioData.audio3DMaxDistance;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// 默认3D最大距离为3米
|
|
||||||
audioSource2.maxDistance = 3f;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
audioSource2.transform.position = Vector3.zero;
|
// 默认3D最大距离为3米
|
||||||
audioSource2.spatialBlend = 0;
|
audioSource2.maxDistance = 3f;
|
||||||
audioSource2.minDistance = 1f;
|
|
||||||
audioSource2.maxDistance = 500f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
currentAudioSource = audioSource2;
|
|
||||||
currentAudioSource.Play();
|
|
||||||
FadeMusic(audioSource2, audioData.fadeDuration, audioSource1);
|
|
||||||
}
|
}
|
||||||
else
|
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对象
|
/// 创建一个新的AudioSource对象
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="obj"></param>
|
/// <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 = new GameObject(name).AddComponent<AudioSource>();
|
||||||
source.gameObject.transform.SetParent(poolObject.transform);
|
source.gameObject.transform.SetParent(poolObject.transform);
|
||||||
source.transform.SetParent(poolObject.transform); // 将新对象作为当前对象的子对象
|
source.transform.SetParent(poolObject.transform); // 将新对象作为当前对象的子对象
|
||||||
source.playOnAwake = false; // 添加 AudioSource 组件并禁用自动播放
|
source.playOnAwake = false; // 添加 AudioSource 组件并禁用自动播放
|
||||||
return source;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "com.staryevo.audiocore",
|
"name": "com.staryevo.audiocore",
|
||||||
"version": "1.1.1",
|
"version": "1.1.2",
|
||||||
"displayName": "04.AudioCore",
|
"displayName": "04.AudioCore",
|
||||||
"description": "音频播放工具",
|
"description": "音频播放工具",
|
||||||
"unity": "2021.3",
|
"unity": "2021.3",
|
||||||
|
|||||||
Reference in New Issue
Block a user