Unity3d5.0之后關(guān)于游戲音樂的管理-創(chuàng)新互聯(lián)

首先這里我們只要把這個游戲管理器做成預(yù)制件,然后寫一個通用模板類(包括關(guān)閉音樂和開啟音樂,關(guān)閉音效和開啟音效,和游戲存檔來開啟關(guān)閉音樂)掛在這個預(yù)制件上面。

成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供赤坎網(wǎng)站建設(shè)、赤坎做網(wǎng)站、赤坎網(wǎng)站設(shè)計、赤坎網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、赤坎企業(yè)網(wǎng)站模板建站服務(wù),十余年赤坎做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

通用模板類:

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");  //這里是加載你的游戲音樂,這里自己寫了一個動態(tài)加載資源的辦法,方便后面資源打包

        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();

    }

  }

  //方便其他腳本調(diào)用這個播放音樂的方法

static public void PlayCurrentMusic(){

if (s_Instance != null) {

PlayMusic(s_Instance.MusicName);

}

}

  //關(guān)閉音樂

  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);

  }

}

現(xiàn)在任何地方想要關(guān)閉或者開啟音樂音效的時候就可以調(diào)用這個游戲管理類里面的

  AudioPlayer.PlayCurrentMusic();開啟音樂

  AudioPlayer.CloseMusic();關(guān)閉音樂

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

名稱欄目:Unity3d5.0之后關(guān)于游戲音樂的管理-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://muchs.cn/article10/dshggo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、服務(wù)器托管網(wǎng)站改版、Google、標簽優(yōu)化外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司