重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
首先这里我们只要把这个游戏管理器做成预制件,然后写一个通用模板类(包括关闭音乐和开启音乐,关闭音效和开启音效,和游戏存档来开启关闭音乐)挂在这个预制件上面。
成都创新互联公司专业为企业提供赤坎网站建设、赤坎做网站、赤坎网站设计、赤坎网站制作等企业网站建设、网页设计与制作、赤坎企业网站模板建站服务,十余年赤坎做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。通用模板类:
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
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
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[s_Instance.currentAudioIndex].clip = Util.LoadResource
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[0].clip = Util.LoadResource
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[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();关闭音乐
另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。