| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- 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);
- }
- public void PlayAnimalEffect(string name, AudioSource audioSource) {
- this.Play("Audios/Animal/" + name, audioSource);
- }
- }
|