Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Diagnostics;
- using Unity.PlasticSCM.Editor.WebApi;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- public class Music : MonoBehaviour
- {
- static Music music;
- [SerializeField] AudioClip menuLoop, gameLoop;
- [SerializeField] AudioSource audioSource;
- string lastScene = "";
- AudioClip newAudioClip = null;
- void Start()
- {
- if (music != null)
- {
- Destroy(gameObject);
- }
- else
- {
- DontDestroyOnLoad(gameObject);
- music = this;
- lastScene = SceneManager.GetActiveScene().name;
- UpdateMusic();
- }
- audioSource.Play();
- }
- void Update()
- {
- string currentScene = SceneManager.GetActiveScene().name;
- if (currentScene != lastScene)
- {
- lastScene = currentScene;
- UpdateMusic();
- }
- }
- public void UpdateMusic()
- {
- switch (SceneManager.GetActiveScene().name)
- {
- case "Menu":
- case "Settings":
- newAudioClip = menuLoop;
- break;
- case "Level 1":
- newAudioClip = gameLoop;
- break;
- default:
- print("No Audio will be playing!");
- break;
- }
- if (newAudioClip != audioSource.clip)
- {
- audioSource.clip = newAudioClip;
- audioSource.Play();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement