|
- using System;
- using System.Collections.Generic;
- using SUISS.Core;
- using UnityEngine;
-
- namespace SUISSEngine
- {
- [RequireComponent(typeof(AudioSource))]
- public abstract class AudioManager<T> : SingletonMonobehaviour<T> where T : AudioManager<T>
- {
- public void Mute()
- {
- this._muted = true;
- if (this._musicEnabled)
- {
- this._audioSource.Stop();
- }
- }
-
- public void Unmute()
- {
- this._muted = false;
- if (this._musicEnabled)
- {
- this._audioSource.Play();
- }
- }
-
- public void EnableMusic(bool enabled)
- {
- this._musicEnabled = enabled;
- if (!this._muted && this._musicEnabled && !this._audioSource.isPlaying)
- {
- this._audioSource.Play();
- }
- else if ((this._muted || !this._musicEnabled) && this._audioSource.isPlaying)
- {
- this._audioSource.Stop();
- }
- }
-
- public void EnableSFX(bool enabled)
- {
- this._sfxEnabled = enabled;
- }
-
- public void PlayClip(AudioClip clip, float volumeScale)
- {
- this.PlayClip(clip, volumeScale, false);
- }
-
- public void PlayClip(AudioClip clip, float volumeScale, bool playOneAtATime)
- {
- float num;
- if (!this._muted && this._sfxEnabled && (!playOneAtATime || !this._clipLastPlayTime.TryGetValue(clip.name, out num) || Time.realtimeSinceStartup - num > clip.length))
- {
- this._audioSource.PlayOneShot(clip, volumeScale);
- if (playOneAtATime)
- {
- this._clipLastPlayTime[clip.name] = Time.realtimeSinceStartup;
- }
- }
- }
-
- [SerializeField]
- [SelfReference]
- protected AudioSource _audioSource;
-
- private bool _musicEnabled;
-
- private bool _sfxEnabled;
-
- private bool _muted;
-
- private Dictionary<string, float> _clipLastPlayTime = new Dictionary<string, float>();
- }
- }
|