| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- 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<AudioMgr>();
- DontDestroyOnLoad(audioMgr);
- }
- }
- void Start() {
- ins = this;
- this.audioSource = this.gameObject.AddComponent<AudioSource>();
- 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<AudioClip>(path);
- if (audioSource == null) {
- audioSource = this.audioSource;
- }
- audioSource.clip = audioClip;
- if (openEffect) {
- audioSource.Play();
- }
- }
- public static AudioSource GetAudioSource(GameObject target) {
- AudioSource audioSource = target.GetComponent<AudioSource>();
- if (audioSource == null) {
- audioSource = target.AddComponent<AudioSource>();
- }
- 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);
- }
- }
|