| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- namespace DevilLink2.Presentation
- {
- public interface IAudioOutput : IDisposable
- {
- void SwitchMusic(AudioClip clip);
- void SetMusicVolume(float volume);
- void SetSfxVolume(float volume);
- void PlayEffect(AudioClip clip);
- void StopEffects();
- void StopAll();
- }
- public interface ICalibratedAudioOutput
- {
- bool TryPlayEffect(AudioClip clip, AudioCue cue, float gain);
- void Tick();
- }
- [DisallowMultipleComponent]
- public sealed class AudioManager : MonoBehaviour
- {
- public static AudioManager Instance { get; private set; }
- private IAudioOutput output;
- private AudioCatalog catalog;
- private readonly HashSet<string> played = new HashSet<string>();
- private readonly Queue<string> history = new Queue<string>();
- public MusicTrack CurrentMusic { get; private set; }
- public bool MusicEnabled { get; private set; } = true;
- public bool SfxEnabled { get; private set; } = true;
- public float MusicVolume { get; private set; } = AudioMixCalibration.DefaultMusic;
- public float SfxVolume { get; private set; } = AudioMixCalibration.DefaultSfx;
- public bool IsInitialized => output != null;
- public event Action SettingsChanged;
- public static AudioManager Ensure(GameObject owner)
- {
- if (Instance) { if(!Instance.IsInitialized)Instance.Initialize(Resources.Load<AudioCatalog>("DevilLink2AudioCatalog")); return Instance; }
- var manager = owner.GetComponent<AudioManager>(); if (!manager) manager = owner.AddComponent<AudioManager>();
- manager.Initialize(Resources.Load<AudioCatalog>("DevilLink2AudioCatalog")); return manager;
- }
- private void Awake()
- {
- if (Instance && Instance != this) { enabled = false; Destroy(this); return; }
- Instance = this;
- }
- public bool Initialize(AudioCatalog assets, IAudioOutput backend = null)
- {
- if (Instance && Instance != this) return false;
- Instance = this;
- if (output != null) return true;
- catalog = assets; output = backend ?? new UnityAudioOutput(transform, Application.isPlaying);
- ApplyVolumes(); return true;
- }
- public bool SetMusic(MusicTrack track)
- {
- if (!enabled || !gameObject.activeInHierarchy || output == null || CurrentMusic == track) return false;
- var clip = catalog ? catalog.Music(track) : null;
- if (track != MusicTrack.None && !clip) return false;
- CurrentMusic = track; output.SwitchMusic(clip); return true;
- }
- public bool Play(AudioCue cue, string operationKey = null)
- {
- if (!enabled || !gameObject.activeInHierarchy || output == null) return false;
- if (operationKey != null)
- {
- string key = cue + ":" + operationKey;
- if (!played.Add(key)) return false;
- history.Enqueue(key); if (history.Count > 512) played.Remove(history.Dequeue());
- }
- var clip = catalog ? catalog.Effect(cue) : null;
- if (!SfxEnabled || SfxVolume <= 0 || !clip) return false;
- if (output is ICalibratedAudioOutput calibrated) return calibrated.TryPlayEffect(clip, cue, AudioMixCalibration.Gain(cue));
- output.PlayEffect(clip); return true;
- }
- public static void Sound(AudioCue cue, string operationKey = null) { if (Instance) Instance.Play(cue, operationKey); }
- public void SetMusicEnabled(bool value) { if (MusicEnabled == value) return; MusicEnabled = value; ApplyVolumes(); SettingsChanged?.Invoke(); }
- public void SetSfxEnabled(bool value) { if (SfxEnabled == value) return; SfxEnabled = value; if (!value) output?.StopEffects(); ApplyVolumes(); SettingsChanged?.Invoke(); }
- 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(); }
- 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(); }
- private void ApplyVolumes() { output?.SetMusicVolume(MusicEnabled ? MusicVolume : 0); output?.SetSfxVolume(SfxEnabled ? SfxVolume : 0); }
- private void Update() { if (output is ICalibratedAudioOutput calibrated) calibrated.Tick(); }
- private void OnDisable() { output?.StopAll(); CurrentMusic = MusicTrack.None; }
- public void Shutdown() { output?.Dispose(); output = null; CurrentMusic = MusicTrack.None; if (Instance == this) Instance = null; SettingsChanged = null; }
- private void OnDestroy() { Shutdown(); }
- }
- }
|