JamesMakesGames

BackgroundMusic

May 27th, 2022
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class BackgroundMusic : MonoBehaviour
  4. {
  5.     public AudioSource _musicAudioSource;
  6.  
  7.     void Awake()
  8.     {
  9.         GameStateManager.Instance.OnGameStateChanged += OnGameStateChanged;
  10.  
  11.         OnGameStateChanged(GameStateManager.Instance.CurrentGameState);
  12.     }
  13.  
  14.     void OnDestroy()
  15.     {
  16.         GameStateManager.Instance.OnGameStateChanged -= OnGameStateChanged;
  17.     }
  18.  
  19.     private void OnGameStateChanged(GameState newGameState)
  20.     {
  21.         if (newGameState == GameState.Gameplay)
  22.         {
  23.             _musicAudioSource.UnPause();
  24.         }
  25.         else
  26.         {
  27.             _musicAudioSource.Pause();
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment