首先这里我们只要把这个游戏管理器做成预制件,然后写一个通用模板类(包括关闭音乐和开启音乐,关闭音效和开启音效,和游戏存档来开启关闭音乐)挂在这个预制件上面。
通用模板类:
using UnityEngine;
using System.Collections;
public class AudioPlayer : MonoBehaviour{
static public AudioPlayer s_Instance = null;
static public bool IsRetargetOK=true;
//播放音效一次
static public void PlayAudioOnce(string name){
if (PlayerPrefs.HasKey("Audio") && PlayerPrefs.GetInt("Audio") == 0) {
return;
} //这里是存档来开启音乐
if (s_Instance != null){
AudioSource[] audioSource = s_Instance.gameObject.GetComponents<AudioSource>();
bool isPlayOk = true;
foreach (var item in audioSource){
if (item.clip != null && item.clip.name == name&&item.isPlaying){
isPlayOk = false;
break;
}
}
if (isPlayOk){
audioSource[s_Instance.currentAudioIndex].clip = Util.LoadResource<AudioClip>("Sounds/" + name + ".mp3"); //这里是加载你的游戏音乐,这里自己写了一个动态加载资源的办法,方便后面资源打包
audioSource[s_Instance.currentAudioIndex].Play();
s_Instance.currentAudioIndex++;
if (s_Instance.currentAudioIndex > AudioPlayer.kAudioCount){
s_Instance.currentAudioIndex = 1;
}
}else{
}
}
}
//播放音效
static public void PlayAudio(string name){
if (PlayerPrefs.HasKey("Audio") && PlayerPrefs.GetInt("Audio") == 0){
return;
}
if (s_Instance != null){
AudioSource[] audioSource = s_Instance.gameObject.GetComponents<AudioSource>();
audioSource[s_Instance.currentAudioIndex].clip = Util.LoadResource<AudioClip>("Sounds/" + name + ".mp3");
audioSource[s_Instance.currentAudioIndex].Play();
s_Instance.currentAudioIndex++;
if (s_Instance.currentAudioIndex > AudioPlayer.kAudioCount)
{
s_Instance.currentAudioIndex = 1;
}
}
}
//播放音乐
static public void PlayMusic(string name){
if (PlayerPrefs.HasKey("Music") && PlayerPrefs.GetInt("Music") == 0){
return;
}
if (s_Instance != null) {
AudioSource[] audioSource = s_Instance.gameObject.GetComponents<AudioSource>();
audioSource[0].clip = Util.LoadResource<AudioClip>("Sounds/" + name + ".mp3");
audioSource[0].Play();
}
}
//方便其他脚本调用这个播放音乐的方法
static public void PlayCurrentMusic(){
if (s_Instance != null) {
PlayMusic(s_Instance.MusicName);
}
}
//关闭音乐
static public void CloseMusic(){
if (s_Instance != null){
AudioSource[] audioSource = s_Instance.gameObject.GetComponents<AudioSource>();
audioSource[0].Stop();
}
}
private int currentAudioIndex = 1;
public const int kAudioCount = 31;
public string MusicName = "";
//单例模式
void Awake(){
s_Instance = this;
}
//游戏刚安装的时候一进来先播放音乐
void Start(){
AudioPlayer.PlayMusic(MusicName);
}
}
游戏存档代码:
using UnityEngine;
using System.Collections;
using System;
public class SetCanvas : MonoBehaviour
{
public GameObject BtONMusic;
public GameObject BtOFFMusic;
public GameObject BtONAudio;
public GameObject BtOFFAudio;
void Start()
{
if (PlayerPrefs.HasKey("Music") && PlayerPrefs.GetInt("Music") == 0)
{
BtOFFMusic.SetActive(true);
BtONMusic.SetActive(false);
}
else if (PlayerPrefs.HasKey("Music") && PlayerPrefs.GetInt("Music") == 1)
{
BtONMusic.SetActive(true);
BtOFFMusic.SetActive(false);
}
else
{
BtONMusic.SetActive(true);
BtOFFMusic.SetActive(false);
}
if (PlayerPrefs.HasKey("Audio") && PlayerPrefs.GetInt("Audio") == 0)
{
BtOFFAudio.SetActive(true);
BtONAudio.SetActive(false);
}
else if (PlayerPrefs.HasKey("Audio") && PlayerPrefs.GetInt("Audio") == 1)
{
BtOFFAudio.SetActive(false);
BtONAudio.SetActive(true);
}
else
{
BtONAudio.SetActive(true);
BtOFFAudio.SetActive(false);
}
}
public void CloseMusic()
{
PlayerPrefs.SetInt("Music", 0);
AudioPlayer.CloseMusic();
BtOFFMusic.SetActive(true);
BtONMusic.SetActive(false);
}
public void OpenMusic()
{
PlayerPrefs.SetInt("Music", 1);
AudioPlayer.PlayCurrentMusic();
BtOFFMusic.SetActive(false);
BtONMusic.SetActive(true);
}
public void CloseAudio()
{
PlayerPrefs.SetInt("Audio", 0);
BtOFFAudio.SetActive(true);
BtONAudio.SetActive(false);
}
public void OpenAudio()
{
PlayerPrefs.SetInt("Audio", 1);
BtOFFAudio.SetActive(false);
BtONAudio.SetActive(true);
}
public void ColseUI()
{
gameObject.SetActive(false);
}
}
现在任何地方想要关闭或者开启音乐音效的时候就可以调用这个游戏管理类里面的
AudioPlayer.PlayCurrentMusic();开启音乐
AudioPlayer.CloseMusic();关闭音乐