Implement working BGM player
This commit is contained in:
33
Assets/Scripts/Audio/BGM.cs
Normal file
33
Assets/Scripts/Audio/BGM.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Scampz.GameJam.Assets.Scripts.Audio
|
||||
{
|
||||
public class BGM : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private AudioClip[] backgroundMusic;
|
||||
|
||||
public AudioClip GetClipFromSceneType(Scene scene)
|
||||
{
|
||||
AudioClip audioClipToPlay = null;
|
||||
|
||||
if (scene.name.Contains("Temple"))
|
||||
audioClipToPlay = backgroundMusic[0];
|
||||
if (scene.name.Contains("Sanctum"))
|
||||
audioClipToPlay = backgroundMusic[1];
|
||||
if (scene.name.Contains("Snow"))
|
||||
audioClipToPlay = backgroundMusic[2];
|
||||
if (scene.name.Equals("SnowC"))
|
||||
audioClipToPlay = backgroundMusic[3];
|
||||
if (scene.name.Contains("Airship"))
|
||||
audioClipToPlay = backgroundMusic[4];
|
||||
if (scene.name.Equals("AirshipInside"))
|
||||
audioClipToPlay = backgroundMusic[5];
|
||||
if (scene.name.Contains("Void"))
|
||||
audioClipToPlay = backgroundMusic[6];
|
||||
|
||||
return audioClipToPlay;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 492cbb8b94f132345bc45c0c471d3867
|
||||
guid: 62078f3dc064b304a9a033d66fa1157a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace Scampz.GameJam
|
||||
{
|
||||
public class UnloadScene : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private string scene;
|
||||
private bool unloaded = false;
|
||||
|
||||
void OnTriggerEnter(Collider collider)
|
||||
{
|
||||
if (!unloaded)
|
||||
{
|
||||
unloaded = true;
|
||||
GameManager.Instance.UnloadScene(scene);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user