| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  | using System; | 
					
						
							|  |  |  |  | using System.Collections.Generic; | 
					
						
							|  |  |  |  | using UnityEngine; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  | public class AudioManager : D_MonoSingleton<AudioManager> | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-02-09 08:35:01 +00:00
										 |  |  |  |     AudioListener mAudioListener; | 
					
						
							|  |  |  |  |     AudioSource mMusicPlayer; | 
					
						
							|  |  |  |  |     AudioSource mSoundPlayer; | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-16 09:00:22 +00:00
										 |  |  |  |     private Dictionary<AudioClipType, List<AudioClip>> mAudioDic = new Dictionary<AudioClipType, List<AudioClip>>(); | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     protected override void Initialize() | 
					
						
							|  |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |         for (int i = 0; i < AudioConfig.Instance.AudioPacks.Count; i++) | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |         { | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |             AudioPack tPack = AudioConfig.Instance.AudioPacks[i]; | 
					
						
							|  |  |  |  |             if (!mAudioDic.ContainsKey(tPack.myType)) | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |             { | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |                 mAudioDic[tPack.myType] = new List<AudioClip>(); | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |             mAudioDic[tPack.myType] = tPack.myClips; | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         CheckAudioListner(); | 
					
						
							|  |  |  |  |         CheckMusicPlayer(); | 
					
						
							|  |  |  |  |         CheckSoundPlayer(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |     private void CheckAudioListner() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         if (mAudioListener == null) | 
					
						
							|  |  |  |  |             mAudioListener = gameObject.AddComponent<AudioListener>(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     private void CheckMusicPlayer() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         if (mMusicPlayer == null) | 
					
						
							|  |  |  |  |             mMusicPlayer = gameObject.AddComponent<AudioSource>(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     private void CheckSoundPlayer() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         if (mSoundPlayer == null) | 
					
						
							|  |  |  |  |             mSoundPlayer = gameObject.AddComponent<AudioSource>(); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |     //private void Update() | 
					
						
							|  |  |  |  |     //{ | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |     //    if (AudioListener.pause && mIsListenerOn) | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |     //    { | 
					
						
							|  |  |  |  |     //        AudioListener.pause = false; | 
					
						
							|  |  |  |  |     //    } | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |     //    else if (!AudioListener.pause && !mIsListenerOn) | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |     //    { | 
					
						
							|  |  |  |  |     //        AudioListener.pause = true; | 
					
						
							|  |  |  |  |     //    } | 
					
						
							|  |  |  |  |     //} | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |     public void SetListenerOn(bool pIsOn) | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |     { | 
					
						
							|  |  |  |  |         AudioListener.pause = !pIsOn; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |     public void SetMusicOn(bool pIsOn) | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |         mMusicPlayer.enabled = pIsOn; | 
					
						
							|  |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |     public void SetSoundOn(bool pIsOn) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         mSoundPlayer.enabled = pIsOn; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-16 09:00:22 +00:00
										 |  |  |  |     public void PlayBGMusic(AudioClipType pType, float pVolumeScale = 1) | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |         CheckMusicPlayer(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         AudioClip tClip = GetClip(pType); | 
					
						
							|  |  |  |  |         if (tClip == null) | 
					
						
							|  |  |  |  |             return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 09:14:06 +00:00
										 |  |  |  |         if (mMusicPlayer.enabled) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             mMusicPlayer.clip = tClip; | 
					
						
							|  |  |  |  |             mMusicPlayer.loop = true; | 
					
						
							|  |  |  |  |             mMusicPlayer.volume *= pVolumeScale; | 
					
						
							|  |  |  |  |             mMusicPlayer.Play(); | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public void StopBGMusic() | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         CheckMusicPlayer(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 09:14:06 +00:00
										 |  |  |  |         if (mMusicPlayer.enabled) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             mMusicPlayer.Stop(); | 
					
						
							|  |  |  |  |             mMusicPlayer.clip = null; | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-16 09:00:22 +00:00
										 |  |  |  |     public void PlaySound(AudioClipType pType, float pVolumeScale = 1) | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |     { | 
					
						
							|  |  |  |  |         CheckSoundPlayer(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         AudioClip tClip = GetClip(pType); | 
					
						
							|  |  |  |  |         if (tClip == null) | 
					
						
							|  |  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2022-02-09 08:35:01 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 09:14:06 +00:00
										 |  |  |  |         if (mSoundPlayer.enabled) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             mSoundPlayer.PlayOneShot(tClip, pVolumeScale); | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-02-09 08:35:01 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public void PlaySound(AudioClip pClip, float pVolumeScale = 1) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         CheckSoundPlayer(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         AudioClip tClip = pClip; | 
					
						
							|  |  |  |  |         if (tClip == null) | 
					
						
							|  |  |  |  |             return; | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 09:14:06 +00:00
										 |  |  |  |         if (mSoundPlayer.enabled) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             mSoundPlayer.PlayOneShot(tClip, pVolumeScale); | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-16 09:00:22 +00:00
										 |  |  |  |     public void PlaySound3D(AudioSource pAudioSrc, AudioClipType pType) | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |         AudioClip tClip = GetClip(pType); | 
					
						
							|  |  |  |  |         if (tClip == null) | 
					
						
							|  |  |  |  |             return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 09:14:06 +00:00
										 |  |  |  |         if (pAudioSrc.enabled) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             pAudioSrc.PlayOneShot(tClip); | 
					
						
							|  |  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-16 09:00:22 +00:00
										 |  |  |  |     public AudioSource PlaySoundLoop(AudioClipType pType, float pVolumeScale = 1) | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |         AudioClip tClip = GetClip(pType); | 
					
						
							|  |  |  |  |         if (tClip == null) | 
					
						
							|  |  |  |  |             return null; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         AudioSource tLoopAudio = gameObject.AddComponent<AudioSource>(); | 
					
						
							|  |  |  |  |         tLoopAudio.clip = tClip; | 
					
						
							|  |  |  |  |         tLoopAudio.loop = true; | 
					
						
							|  |  |  |  |         tLoopAudio.volume *= pVolumeScale; | 
					
						
							|  |  |  |  |         tLoopAudio.Play(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         return tLoopAudio; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     public void StopSoundLoop(AudioSource pAudioSrc) | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         if (pAudioSrc == null) | 
					
						
							|  |  |  |  |             return; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         pAudioSrc.Stop(); | 
					
						
							|  |  |  |  |         Destroy(pAudioSrc); | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-16 09:00:22 +00:00
										 |  |  |  |     private AudioClip GetClip(AudioClipType pType) | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |         if (!mAudioDic.ContainsKey(pType) || mAudioDic[pType].Count == 0) | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |             return null; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-10 07:31:53 +00:00
										 |  |  |  |         List<AudioClip> tClips = mAudioDic[pType]; | 
					
						
							| 
									
										
										
										
											2022-01-26 07:46:33 +00:00
										 |  |  |  |         int tRandomPlayIndex = UnityEngine.Random.Range(0, tClips.Count); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         return tClips[tRandomPlayIndex]; | 
					
						
							|  |  |  |  |     } | 
					
						
							|  |  |  |  | } |