using UnityEngine; using Sirenix.OdinInspector; using HGR.Core.Singletons; using System.Collections.Generic; public enum SFX { Click = 0, Spawn = 1, }//enum end [System.Serializable] public sealed class SFXClip { //=================================================== // FIELDS //=================================================== [HorizontalGroup("Split")] [VerticalGroup("Split/Enabled")] [DisplayAsString] [HideLabel] [SerializeField] SFX _sfx; [VerticalGroup("Split/Disabled")] [HideLabel] [SerializeField] AudioClip _clip = null; // Constructor public SFXClip(SFX sfx) => _sfx = sfx; //=================================================== // PROPERTIES //=================================================== public SFX SFX => _sfx; public AudioClip Clip => _clip; }//struct end public sealed class AudioManager : Singleton { //=================================================== // FIELDS //=================================================== [Title("AUDIO MANAGER", titleAlignment: TitleAlignments.Centered)] [SerializeField] AudioSource _bgSource = null; [SerializeField] AudioSource _sfxSource = null; [Space] [SerializeField] AudioClip _bgMusic = null; [Space] [ListDrawerSettings(DraggableItems = false, HideAddButton = true, HideRemoveButton = true, ShowFoldout = true)] [InlineButton(nameof(Create))] [LabelText("SFX Clips")] [SerializeField] List _sfxClips = new List(); //=================================================== // METHODS //=================================================== internal override void Init() { base.Init(); if(InstanceNotSelf) return; SetBGSetting(SaveData.Instance.Music); SetSFXSetting(SaveData.Instance.SFX); }//Start() end /// /// Method for creating Array of SFX Clips. /// private void Create() { int length = System.Enum.GetValues(typeof(SFX)).Length; if(_sfxClips == null || _sfxClips.Count == 0) { _sfxClips = new List(); for(int i=0 ; i /// Toggle Background Music Audio Source. /// public void SetBGSetting(bool Toggle) => _bgSource.mute = !Toggle; /// /// Toggle SFX Audio Source. /// public void SetSFXSetting(bool Toggle) => _sfxSource.mute = !Toggle; /// /// Call when Game Starts to play Background Music. /// public void StartGame() { if(_bgSource.isPlaying) return; _bgSource.clip = _bgMusic; _bgSource.loop = true; _bgSource.Play(); }//StartGame() end /// /// Call when Game Starts to stop playing Background Music. /// public void GameEnd() => _bgSource.Stop(); /// /// Call to play specific SFX clip against enum. /// public void PlaySFX(SFX sfx, float volume = 1f) => _sfxSource.PlayOneShot(_sfxClips[(int)sfx].Clip, volume); /// /// Call to play custom Audio Clip. /// public void PlaySFX(AudioClip clip, float volume = 1f) => _sfxSource.PlayOneShot(clip, volume); }//class end