-
-
Save SetumupJoe/b4fc303ae995711132ba7c1562f1c687 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine.SceneManagement; | |
using UnityEngine; | |
public class MusicPlayer : MonoBehaviour | |
{ | |
static MusicPlayer instance = null; | |
AudioSource audioSource; | |
public LevelManager levelManager; | |
public AudioClip splashTheme; | |
public AudioClip mainMenuTheme; | |
public AudioClip levelOneTheme; | |
public AudioClip levelTwoTheme; | |
public AudioClip levelThreeTheme; | |
public AudioClip normalEndingTheme; | |
public AudioClip settingsTheme; | |
public AudioClip perfectEndingTheme; | |
public AudioClip gameOverTheme; | |
public AudioClip storyBookTheme; | |
[HideInInspector] | |
public int thisSceneIndex; | |
[HideInInspector] | |
public int lastSceneIndex; | |
private AudioSource music; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
levelManager = FindObjectOfType<LevelManager>(); | |
DontDestroyOnLoad(this); | |
audioSource = GetComponent<AudioSource>(); | |
audioSource.volume = PlayerPrefsController.GetMasterVolume(); | |
if (instance != null && instance != this) | |
{ | |
Destroy(gameObject); | |
print("Duplicate music player self-destructing!"); | |
} | |
else | |
{ | |
instance = this; | |
GameObject.DontDestroyOnLoad(gameObject); | |
music = GetComponent<AudioSource>(); | |
music.clip = splashTheme; | |
music.loop = true; | |
music.Play(); | |
} | |
lastSceneIndex = SceneManager.GetActiveScene().buildIndex; | |
} | |
private void Update() | |
{ | |
thisSceneIndex = SceneManager.GetActiveScene().buildIndex; | |
if(thisSceneIndex != lastSceneIndex) | |
{ | |
ChangeSong(); | |
lastSceneIndex = thisSceneIndex; | |
} | |
} | |
public void SetVolume(float volume) | |
{ | |
audioSource.volume = volume; | |
} | |
void ChangeSong() | |
{ | |
if(levelManager.currentSceneIndex == 0) | |
{ | |
music.clip = splashTheme; | |
music.loop = true; | |
audioSource.Play(); | |
} | |
else if (levelManager.currentSceneIndex == 1) | |
{ | |
music.clip = mainMenuTheme; | |
music.loop = true; | |
audioSource.Play(); | |
} | |
else if (levelManager.currentSceneIndex == 2) | |
{ | |
music.clip = levelOneTheme; | |
music.loop = true; | |
audioSource.Play(); | |
} | |
else if (levelManager.currentSceneIndex == 3) | |
{ | |
music.clip = levelTwoTheme; | |
music.loop = true; | |
audioSource.Play(); | |
} | |
else if (levelManager.currentSceneIndex == 4) | |
{ | |
music.clip = levelThreeTheme; | |
music.loop = true; | |
audioSource.Play(); | |
} | |
else if (levelManager.currentSceneIndex == 5) | |
{ | |
music.clip = normalEndingTheme; | |
music.loop = false; | |
audioSource.Play(); | |
} | |
else if (levelManager.currentSceneIndex == 6) | |
{ | |
music.clip = settingsTheme; | |
music.loop = true; | |
audioSource.Play(); | |
} | |
else if (levelManager.currentSceneIndex == 7) | |
{ | |
music.clip = perfectEndingTheme; | |
music.loop = false; | |
audioSource.Play(); | |
} | |
else if (levelManager.currentSceneIndex == 8) | |
{ | |
music.clip = gameOverTheme; | |
music.loop = false; | |
audioSource.Play(); | |
} | |
else if (levelManager.currentSceneIndex == 9) | |
{ | |
music.clip = storyBookTheme; | |
music.loop = true; | |
audioSource.Play(); | |
} | |
else | |
{ | |
ChangeSong(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment