改动与优化
This commit is contained in:
@@ -192,7 +192,10 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
}
|
||||
|
||||
if (audioSource.clip != null && !audioSource.isPlaying)
|
||||
{
|
||||
audioSource.Play();
|
||||
StopLegacyMusicManagerSources();
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator LoadClipRoutine()
|
||||
@@ -248,7 +251,10 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
}
|
||||
|
||||
if (audioSource.clip != null && !audioSource.isPlaying)
|
||||
{
|
||||
audioSource.Play();
|
||||
StopLegacyMusicManagerSources();
|
||||
}
|
||||
|
||||
if (loadClipRoutine != null)
|
||||
{
|
||||
@@ -342,6 +348,45 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
audioSource.loop = loop;
|
||||
audioSource.time = 0f;
|
||||
audioSource.Play();
|
||||
StopLegacyMusicManagerSources();
|
||||
}
|
||||
|
||||
public void PlayExternalClip(AudioClip externalClip, bool restart)
|
||||
{
|
||||
if (externalClip == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ShouldPlayInScene(SceneManager.GetActiveScene().name))
|
||||
{
|
||||
StopLobbyAudioAndParticles();
|
||||
return;
|
||||
}
|
||||
|
||||
if (audioSource == null)
|
||||
{
|
||||
audioSource = gameObject.AddComponent<AudioSource>();
|
||||
}
|
||||
|
||||
ApplyOutputMixerGroup();
|
||||
|
||||
bool clipChanged = audioSource.clip != externalClip;
|
||||
clip = externalClip;
|
||||
audioSource.clip = externalClip;
|
||||
audioSource.loop = loop;
|
||||
|
||||
if (restart || clipChanged)
|
||||
{
|
||||
audioSource.time = 0f;
|
||||
}
|
||||
|
||||
if (!audioSource.isPlaying || restart || clipChanged)
|
||||
{
|
||||
audioSource.Play();
|
||||
}
|
||||
|
||||
StopLegacyMusicManagerSources();
|
||||
}
|
||||
|
||||
public void Next()
|
||||
@@ -400,6 +445,24 @@ public class BgmPlaybackManager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private void StopLegacyMusicManagerSources()
|
||||
{
|
||||
var managers = FindObjectsByType<Music_Mgr>(FindObjectsInactive.Include, FindObjectsSortMode.None);
|
||||
for (int i = 0; i < managers.Length; i++)
|
||||
{
|
||||
Music_Mgr manager = managers[i];
|
||||
if (manager == null || manager.AudioSource == null || manager.AudioSource == audioSource)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (manager.AudioSource.isPlaying)
|
||||
{
|
||||
manager.AudioSource.Stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public float CurrentTime => audioSource != null ? audioSource.time : 0f;
|
||||
public float TotalTime => (audioSource != null && audioSource.clip != null) ? audioSource.clip.length : 0f;
|
||||
public bool IsReady => audioSource != null && audioSource.clip != null;
|
||||
|
||||
Reference in New Issue
Block a user