using System.Collections; using System.Collections.Generic; using UnityEngine; public class AudioMgr : MonoBehaviour { private AudioSource audioSource; public static AudioMgr ins; public static bool openEffect = true; public static bool openBGM = true; public static void Init() { if (GameObject.Find("AudioMgr") == null) { GameObject audioMgr = new GameObject("AudioMgr"); audioMgr.AddComponent(); DontDestroyOnLoad(audioMgr); } } void Start() { ins = this; this.audioSource = this.gameObject.AddComponent(); openEffect = PlayerPrefs.GetInt("openEffect", 1) == 1 ? true: false; openBGM = PlayerPrefs.GetInt("openBGM", 1) == 1 ? true: false; } void OnDestroy() { PlayerPrefs.SetInt("openEffect", openEffect ? 1 : 0); PlayerPrefs.SetInt("openBGM", openBGM ? 1 : 0); } private void Play(string path, AudioSource audioSource) { AudioClip audioClip = Resources.Load(path); if (audioSource == null) { audioSource = this.audioSource; } audioSource.clip = audioClip; if (openEffect) { audioSource.Play(); } } public static AudioSource GetAudioSource(GameObject target) { AudioSource audioSource = target.GetComponent(); if (audioSource == null) { audioSource = target.AddComponent(); } return audioSource; } public void PlayShoot(AudioSource audioSource) { this.Play("Audios/shoot", audioSource); } public void PlayHit(AudioSource audioSource) { this.Play("Audios/hit", audioSource); } public void PlayCheer(bool cheer) { this.Play("Audios/" + (cheer ? "喝彩" : "喝倒彩"), null); } public void PlayBtn() { this.Play("Audios/btn", null); } public void PlayWin() { this.Play("Audios/win", null); } }