You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

78 lines
1.7 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using SUISS.Core;
  4. using UnityEngine;
  5. namespace SUISSEngine
  6. {
  7. [RequireComponent(typeof(AudioSource))]
  8. public abstract class AudioManager<T> : SingletonMonobehaviour<T> where T : AudioManager<T>
  9. {
  10. public void Mute()
  11. {
  12. this._muted = true;
  13. if (this._musicEnabled)
  14. {
  15. this._audioSource.Stop();
  16. }
  17. }
  18. public void Unmute()
  19. {
  20. this._muted = false;
  21. if (this._musicEnabled)
  22. {
  23. this._audioSource.Play();
  24. }
  25. }
  26. public void EnableMusic(bool enabled)
  27. {
  28. this._musicEnabled = enabled;
  29. if (!this._muted && this._musicEnabled && !this._audioSource.isPlaying)
  30. {
  31. this._audioSource.Play();
  32. }
  33. else if ((this._muted || !this._musicEnabled) && this._audioSource.isPlaying)
  34. {
  35. this._audioSource.Stop();
  36. }
  37. }
  38. public void EnableSFX(bool enabled)
  39. {
  40. this._sfxEnabled = enabled;
  41. }
  42. public void PlayClip(AudioClip clip, float volumeScale)
  43. {
  44. this.PlayClip(clip, volumeScale, false);
  45. }
  46. public void PlayClip(AudioClip clip, float volumeScale, bool playOneAtATime)
  47. {
  48. float num;
  49. if (!this._muted && this._sfxEnabled && (!playOneAtATime || !this._clipLastPlayTime.TryGetValue(clip.name, out num) || Time.realtimeSinceStartup - num > clip.length))
  50. {
  51. this._audioSource.PlayOneShot(clip, volumeScale);
  52. if (playOneAtATime)
  53. {
  54. this._clipLastPlayTime[clip.name] = Time.realtimeSinceStartup;
  55. }
  56. }
  57. }
  58. [SerializeField]
  59. [SelfReference]
  60. protected AudioSource _audioSource;
  61. private bool _musicEnabled;
  62. private bool _sfxEnabled;
  63. private bool _muted;
  64. private Dictionary<string, float> _clipLastPlayTime = new Dictionary<string, float>();
  65. }
  66. }