AudioManager.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace DevilLink2.Presentation
  5. {
  6. public interface IAudioOutput : IDisposable
  7. {
  8. void SwitchMusic(AudioClip clip);
  9. void SetMusicVolume(float volume);
  10. void SetSfxVolume(float volume);
  11. void PlayEffect(AudioClip clip);
  12. void StopEffects();
  13. void StopAll();
  14. }
  15. public interface ICalibratedAudioOutput
  16. {
  17. bool TryPlayEffect(AudioClip clip, AudioCue cue, float gain);
  18. void Tick();
  19. }
  20. [DisallowMultipleComponent]
  21. public sealed class AudioManager : MonoBehaviour
  22. {
  23. public static AudioManager Instance { get; private set; }
  24. private IAudioOutput output;
  25. private AudioCatalog catalog;
  26. private readonly HashSet<string> played = new HashSet<string>();
  27. private readonly Queue<string> history = new Queue<string>();
  28. public MusicTrack CurrentMusic { get; private set; }
  29. public bool MusicEnabled { get; private set; } = true;
  30. public bool SfxEnabled { get; private set; } = true;
  31. public float MusicVolume { get; private set; } = AudioMixCalibration.DefaultMusic;
  32. public float SfxVolume { get; private set; } = AudioMixCalibration.DefaultSfx;
  33. public bool IsInitialized => output != null;
  34. public event Action SettingsChanged;
  35. public static AudioManager Ensure(GameObject owner)
  36. {
  37. if (Instance) { if(!Instance.IsInitialized)Instance.Initialize(Resources.Load<AudioCatalog>("DevilLink2AudioCatalog")); return Instance; }
  38. var manager = owner.GetComponent<AudioManager>(); if (!manager) manager = owner.AddComponent<AudioManager>();
  39. manager.Initialize(Resources.Load<AudioCatalog>("DevilLink2AudioCatalog")); return manager;
  40. }
  41. private void Awake()
  42. {
  43. if (Instance && Instance != this) { enabled = false; Destroy(this); return; }
  44. Instance = this;
  45. }
  46. public bool Initialize(AudioCatalog assets, IAudioOutput backend = null)
  47. {
  48. if (Instance && Instance != this) return false;
  49. Instance = this;
  50. if (output != null) return true;
  51. catalog = assets; output = backend ?? new UnityAudioOutput(transform, Application.isPlaying);
  52. ApplyVolumes(); return true;
  53. }
  54. public bool SetMusic(MusicTrack track)
  55. {
  56. if (!enabled || !gameObject.activeInHierarchy || output == null || CurrentMusic == track) return false;
  57. var clip = catalog ? catalog.Music(track) : null;
  58. if (track != MusicTrack.None && !clip) return false;
  59. CurrentMusic = track; output.SwitchMusic(clip); return true;
  60. }
  61. public bool Play(AudioCue cue, string operationKey = null)
  62. {
  63. if (!enabled || !gameObject.activeInHierarchy || output == null) return false;
  64. if (operationKey != null)
  65. {
  66. string key = cue + ":" + operationKey;
  67. if (!played.Add(key)) return false;
  68. history.Enqueue(key); if (history.Count > 512) played.Remove(history.Dequeue());
  69. }
  70. var clip = catalog ? catalog.Effect(cue) : null;
  71. if (!SfxEnabled || SfxVolume <= 0 || !clip) return false;
  72. if (output is ICalibratedAudioOutput calibrated) return calibrated.TryPlayEffect(clip, cue, AudioMixCalibration.Gain(cue));
  73. output.PlayEffect(clip); return true;
  74. }
  75. public static void Sound(AudioCue cue, string operationKey = null) { if (Instance) Instance.Play(cue, operationKey); }
  76. public void SetMusicEnabled(bool value) { if (MusicEnabled == value) return; MusicEnabled = value; ApplyVolumes(); SettingsChanged?.Invoke(); }
  77. public void SetSfxEnabled(bool value) { if (SfxEnabled == value) return; SfxEnabled = value; if (!value) output?.StopEffects(); ApplyVolumes(); SettingsChanged?.Invoke(); }
  78. public void SetMusicVolume(float value) { if (float.IsNaN(value) || float.IsInfinity(value)) return; value = Mathf.Clamp01(value); if (MusicVolume == value) return; MusicVolume = value; ApplyVolumes(); SettingsChanged?.Invoke(); }
  79. public void SetSfxVolume(float value) { if (float.IsNaN(value) || float.IsInfinity(value)) return; value = Mathf.Clamp01(value); if (SfxVolume == value) return; SfxVolume = value; ApplyVolumes(); SettingsChanged?.Invoke(); }
  80. private void ApplyVolumes() { output?.SetMusicVolume(MusicEnabled ? MusicVolume : 0); output?.SetSfxVolume(SfxEnabled ? SfxVolume : 0); }
  81. private void Update() { if (output is ICalibratedAudioOutput calibrated) calibrated.Tick(); }
  82. private void OnDisable() { output?.StopAll(); CurrentMusic = MusicTrack.None; }
  83. public void Shutdown() { output?.Dispose(); output = null; CurrentMusic = MusicTrack.None; if (Instance == this) Instance = null; SettingsChanged = null; }
  84. private void OnDestroy() { Shutdown(); }
  85. }
  86. }