61 lines
1.4 KiB
C#
61 lines
1.4 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
namespace Scampz.GameJam.Assets.Scripts
|
|
{
|
|
public class BGMManager : MonoBehaviour
|
|
{
|
|
|
|
[SerializeField]
|
|
private AudioClip[] backgroundMusic;
|
|
|
|
private static BGMManager _instance;
|
|
private static AudioSource _audioSource;
|
|
|
|
public static BGMManager Instance
|
|
{
|
|
get
|
|
{
|
|
if (!_instance)
|
|
{
|
|
_instance = new GameObject().AddComponent<BGMManager>();
|
|
_instance.name = _instance.GetType().ToString();
|
|
_audioSource = new GameObject().AddComponent<AudioSource>();
|
|
DontDestroyOnLoad(_instance.gameObject);
|
|
DontDestroyOnLoad(_audioSource.gameObject);
|
|
}
|
|
|
|
return _instance;
|
|
}
|
|
}
|
|
|
|
public void PlaySong()
|
|
{
|
|
_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();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|