单例模式,实现音效管理类

音乐是游戏的灵魂,恰当的音效能给玩家正反馈。利用单例模式,实现音效管理类,简洁实用。只需要在触发音效时用AudioManager._instance.播放具体音效的函数();

比如 if (Input.GetKey(KeyCode.K))
        {
                AudioManager._instance.PlayAttack();//播放攻击音效
            }

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager : MonoBehaviour {
    public static AudioManager _instance;
    private AudioSource audio;

    public AudioClip coin;
    public AudioClip ruby;
    public AudioClip flower;
    public AudioClip jump;
    public AudioClip attack;
    public AudioClip block;
    public AudioClip death;
    public AudioClip enemyDeath;
    public AudioClip background;
    public AudioClip victory;
    public AudioClip defeat;

    void Awake() {
        _instance = this;
        audio = GetComponent();
    }
    //金币
    public void PlayCoin()
    {
        audio.PlayOneShot(coin);
    }
    //宝石
    public void PlayRuby()
    {
        audio.PlayOneShot(ruby);
    }
    //花
    public void PlayFlower()
    {
        audio.PlayOneShot(flower);
    }
    //跳跃
    public void PlayJump()
    {
        audio.PlayOneShot(jump);
    }
    //砖块
    public void PlayBlock()
    {
        audio.PlayOneShot(block);
    }
    //光波
    public void PlayAttack()
    {
        audio.PlayOneShot(attack);
    }
    //死亡
    public void PlayDeath()
    {
        audio.PlayOneShot(death);
    }
    //怪物死亡
    public void PlayEnemyDeath()
    {
        audio.PlayOneShot(enemyDeath);
    }
    //胜利
    public void PlayVictory()
    {
        audio.PlayOneShot(victory);
    }
    //失败
    public void PlayDefeat()
    {
        audio.PlayOneShot(defeat);
    }
    //背景
    public void PlayBackground()
    {
        audio.PlayOneShot(background);
    }
}

 

 

如果文章对你有帮助,请她喝杯奶茶吧~

你可能感兴趣的:(游戏开发,Unity3d)