| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace DuckHunter
- {
- public class AudioManager : MonoBehaviour
- {
- private AudioSource _audioSource;
- private static AudioManager _Instance;
- public static AudioManager Instance
- {
- get
- {
- if (!_Instance) new GameObject(typeof(AudioManager).Name).AddComponent<AudioManager>();
- return _Instance;
- }
- }
- void Awake()
- {
- _Instance = this;
- _audioSource = gameObject.AddComponent<AudioSource>();
- }
- void OnDestroy()
- {
- if (_Instance == this) _Instance = null;
- }
- public void PlayShoot(GameObject target)
- {
- PlayAudio("DuckHunter/Audios/Shoot", GetAudioSource(target));
- }
- public void PlayDuckHit(GameObject target)
- {
- PlayAudio("DuckHunter/Audios/DuckHit", GetAudioSource(target));
- }
- public void PlayDogTaunt(GameObject target)
- {
- PlayAudio("DuckHunter/Audios/DogTaunt", GetAudioSource(target));
- }
- public void PlayDogHappy(GameObject target)
- {
- PlayAudio("DuckHunter/Audios/DogHappy", GetAudioSource(target));
- }
- public void PlayGameStart()
- {
- PlayAudio("DuckHunter/Audios/GameStart", null);
- }
- public void PlayGameOver()
- {
- PlayAudio("DuckHunter/Audios/GameOver", null);
- }
- public void PlayGamePass()
- {
- PlayAudio("DuckHunter/Audios/GamePass", null);
- }
- public void PlayFullScore()
- {
- PlayAudio("DuckHunter/Audios/FullScore", null);
- }
- public void PlayBtn()
- {
- AudioMgr.ins.PlayBtn();
- // PlayAudio("DuckHunter/Audios/Btn", null);
- }
- private void PlayAudio(string audioPath, AudioSource audioSource)
- {
- AudioClip audioClip = Resources.Load<AudioClip>(audioPath);
- if (audioSource == null) audioSource = _audioSource;
- audioSource.clip = audioClip;
- audioSource.Play();
- }
- private AudioSource GetAudioSource(GameObject target)
- {
- if (target == null) return null;
- AudioSource audioSource = target.GetComponent<AudioSource>();
- if (audioSource == null) audioSource = target.AddComponent<AudioSource>();
- return audioSource;
- }
- }
- }
|