Implement working BGM player
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Scampz.GameJam.Assets.Scripts.Audio;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
@@ -9,52 +8,64 @@ namespace Scampz.GameJam.Assets.Scripts
|
||||
{
|
||||
|
||||
[SerializeField]
|
||||
private AudioClip[] backgroundMusic;
|
||||
private BGM _backgroundMusic;
|
||||
|
||||
private static BGMManager _instance;
|
||||
private static AudioSource _audioSource;
|
||||
|
||||
public static BGMManager Instance
|
||||
private void Awake()
|
||||
{
|
||||
get
|
||||
if (_audioSource == null)
|
||||
{
|
||||
if (!_instance)
|
||||
{
|
||||
_instance = new GameObject().AddComponent<BGMManager>();
|
||||
_instance.name = _instance.GetType().ToString();
|
||||
_audioSource = new GameObject().AddComponent<AudioSource>();
|
||||
DontDestroyOnLoad(_instance.gameObject);
|
||||
DontDestroyOnLoad(_audioSource.gameObject);
|
||||
}
|
||||
_audioSource = new GameObject().AddComponent<AudioSource>();
|
||||
_audioSource.loop = true;
|
||||
DontDestroyOnLoad(_audioSource.gameObject);
|
||||
}
|
||||
|
||||
return _instance;
|
||||
if (_backgroundMusic == null)
|
||||
{
|
||||
Instantiate(_backgroundMusic);
|
||||
DontDestroyOnLoad(_backgroundMusic.gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public void PlaySong()
|
||||
private void OnEnable()
|
||||
{
|
||||
SceneManager.activeSceneChanged += PlayNextSongIfAreaChange;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
SceneManager.activeSceneChanged -= PlayNextSongIfAreaChange;
|
||||
}
|
||||
|
||||
private void PlayNextSongIfAreaChange(Scene oldScene, Scene newScene)
|
||||
{
|
||||
if (_audioSource.clip == null)
|
||||
{
|
||||
var newMusic = _backgroundMusic.GetClipFromSceneType(newScene);
|
||||
PlayBGM(newMusic);
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Log($"Changed scenes to " + newScene.name);
|
||||
var newAudioClip = _backgroundMusic.GetClipFromSceneType(newScene);
|
||||
|
||||
if (_audioSource.clip.name != newAudioClip.name)
|
||||
{
|
||||
PlayBGM(newAudioClip);
|
||||
}
|
||||
Debug.Log($"Playing BGM: " + _backgroundMusic.name);
|
||||
|
||||
}
|
||||
|
||||
public void PlayBGM(AudioClip audioClip)
|
||||
{
|
||||
if (_audioSource == null && _audioSource.clip == audioClip)
|
||||
return;
|
||||
|
||||
_audioSource.Stop();
|
||||
|
||||
var sceneCount = SceneManager.sceneCount;
|
||||
var scenes = new List<Scene>();
|
||||
|
||||
for (var i = 0; i < sceneCount; ++i)
|
||||
scenes.Add(SceneManager.GetSceneAt(i));
|
||||
|
||||
AudioClip audioClipToPlay = null;
|
||||
|
||||
if (scenes.Any(x => x.name.Contains("Temple")))
|
||||
audioClipToPlay = backgroundMusic[0];
|
||||
if (scenes.Any(x => x.name.Contains("Sanctum")))
|
||||
audioClipToPlay = backgroundMusic[1];
|
||||
|
||||
if (audioClipToPlay != _audioSource.clip)
|
||||
{
|
||||
_audioSource.clip = audioClipToPlay;
|
||||
_audioSource.Play();
|
||||
}
|
||||
_audioSource.clip = audioClip;
|
||||
_audioSource.Play();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user