BorisKotlyar

Untitled

Dec 1st, 2023
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.68 KB | Gaming | 0 0
  1.     public class MusicController
  2.     {
  3.         private FMOD.Studio.EventInstance _eventInstance;
  4.  
  5.         public MusicController()
  6.         {
  7.             if (PlayerPrefs.HasKey("music_enabled"))
  8.                 return;
  9.  
  10.             Utils.Prefs.SetBool("music_enabled", true);
  11.         }
  12.  
  13.         public void PlayMusic(string musicKey)
  14.         {
  15.             StopMusic();
  16.             if (!Utils.Prefs.GetBool("music_enabled"))
  17.                 return;
  18.  
  19.             _eventInstance = RuntimeManager.CreateInstance(musicKey);
  20.             _eventInstance.start();
  21.         }
  22.  
  23.         public void StopMusic()
  24.         {
  25.             if (!_eventInstance.Equals(default(FMOD.Studio.EventInstance)))
  26.             {
  27.                 _eventInstance.stop(STOP_MODE.ALLOWFADEOUT);
  28.                 _eventInstance.release();
  29.             }
  30.         }
  31.     }
  32.  
  33. // ------------------------------            
  34.         public void Init(Transform parent)
  35.         {
  36.             var window = _uiService.Show<UISettingsWindow>();
  37.             window.MusicToggle.Switch(Utils.Prefs.GetBool("music_enabled"));
  38.             window.MusicToggle.OnValueChanged.AddListener(on =>
  39.             {
  40.                 Utils.Prefs.SetBool("music_enabled", on);
  41.                 if (on)
  42.                     _musicController.PlayMusic("event:/Music/MainMusic");
  43.                 else
  44.                     _musicController.StopMusic();
  45.             });
  46.  
  47.             window.SoundToggle.Switch(Utils.Prefs.GetBool("sound_enabled"));
  48.             window.SoundToggle.OnValueChanged.AddListener(on => Utils.Prefs.SetBool("sound_enabled", on));
  49.  
  50.             window.CloseClickCommand.Subscribe(_ => Dispose()).AddTo(CompositeDisposable);
  51.         }
  52.  
Tags: Settings
Advertisement
Add Comment
Please, Sign In to add comment