Project training (10) - AudioManager sound management

I. Introduction

According to the usual practice, most of the games made by developers have background music and related interactive sound effects. Without audio, the experience and entertainment of the game will be greatly reduced, so we have added suitable background music for each mini-game of the project and sound effects. Because we make a lot of games, if we add music to the corresponding elements individually when making each game, it will make the structure and management of the entire project more confusing. So we learned to make the AudioManager audio manager, and I also learned the reverbs made by other team members. These well-made managers made our project development more orderly and smooth.

Two, AudioManager

The audio management in the game is mainly realized through the AudioManager script.
insert image description here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
using AppKeepLauncher;
using UnityEngine.UI;

public class AudioManager : MonoBehaviour
{
    
    
    public static AudioManager instance;
    /*
    float masterVolumePercent = 1;
    float sfxVoluePercent = 1;
    float musicVolumePercent = 1;
    */
    public AudioMixer audioMixer; //声音混合器
    public GameObject soundSettingsPanel;

    public AudioSpot audioSpot;
    public AudioSource audioSource2D;//2D音效播放器

    [Header("GUI")]
    public AudioClip clickClip;

    [Header("FirstPlatform")]
    public AudioClip explosionClip;

    [Header("PVE")]
    public AudioClip ShootClip;//设计声音
    public AudioClip RelodClip;//重装弹药声音
    public AudioClip thunderClip;
    public AudioClip emergeClip;
    public AudioClip enemyDieClip;
    public AudioClip shockClip;
    public AudioClip playerDieClip;
    public AudioClip winClip;

    [Header("SecondGame")]
    public AudioClip coinTipClip;
    public AudioClip enemyCall;
    public AudioClip cubeAdd;
    public AudioClip cubeDesc;
    public AudioClip SecondVictory;
    public AudioClip failSound;

    [Header("ForthGame")]
    public AudioClip testClip;

    [Header("PropsGame")]
    public AudioClip noneClip;//noProps
    public AudioClip propsGameGetDamageClip;
    public AudioClip startOffMissileClip;
    public AudioClip missileExplosionClip;//same to firstGame
    public AudioClip woodBreakClip;
    public AudioClip cloakingClip;
    public AudioClip flashClip;
    public AudioClip pickingUpClip;//成功捡起道具
    public AudioClip addHealthClip;
    public AudioClip upSpeedClip;//加速
    public AudioClip setLandmineClip;//放置地雷

    #region 混响器设置
    private void Start()
    {
    
    
        if (Launcher.instance.bgmVolume == -100 && Launcher.instance.soundVolume == -100)
        {
    
    
            //audioMixer.GetFloat();
            Launcher.instance.bgmVolume = 10;//***********************应该赋混响器的值
            Launcher.instance.soundVolume = 10;
        }
        else//换场景后保留之前的音量设置
        {
    
    
            audioMixer.SetFloat("bgm1", Launcher.instance.bgmVolume);
            audioMixer.SetFloat("eff1", Launcher.instance.soundVolume);
            //UI
            soundSettingsPanel.transform.GetChild(1).GetComponent<Slider>().value = Launcher.instance.bgmVolume;
            soundSettingsPanel.transform.GetChild(2).GetComponent<Slider>().value = Launcher.instance.soundVolume;
        }
    }
    #endregion

    #region 播放音效的总方法和各个方法
    private void Awake()
    {
    
    
        if(instance ==null)
        {
    
    
            instance = this;
        }
    }


    //*********************************************
    private void PlaySound(AudioClip clip,Vector3 pos)//在指定位置,播放指定音频(3D音效)
    {
    
    
        //AudioSource.PlayClipAtPoint(clip, pos, sfxVoluePercent * masterVolumePercent);
        AudioSpot spot = GameObject.Instantiate(audioSpot, pos, Quaternion.identity);
        spot.PlayClip(clip);
        /*
        audioSource2D.clip = ShootClip;
        audioSource2D.Play();//Pasuse//stop会打断
        audioSource2D.PlayOneShot(RelodClip);//不会打断,可以同时播放
        //static 
        AudioSource.PlayClipAtPoint(ShootClip, pos);//也不能打断,支持同时,静态,注意脱离了对象。
        */
    }
    private void PlaySound2D(AudioClip clip, float volumeScale = 1)//播放指定音频(2D)
    {
    
    
        audioSource2D.PlayOneShot(clip, volumeScale);
    }


    //*********************************************

    #region 第一个游戏
    public void Bomb()
    {
    
    
        PlaySound2D(explosionClip, 2);
    }
    #endregion

    #region 第三个游戏
    public void Shoot(Vector3 pos)
    {
    
    
        PlaySound(ShootClip, pos);
        //new object there and destroy automatic
        
    }
    public void Relod(Vector3 pos)
    {
    
    
        PlaySound(RelodClip, pos);
    }

    public void Thunder(Vector3 pos)
    {
    
    
        PlaySound(thunderClip, pos);
    }

    public void Emerge(Vector3 pos)//******3d出现
    {
    
    
        PlaySound(emergeClip, pos);
    }

    public void EnemyDie(Vector3 pos)
    {
    
    
        PlaySound(enemyDieClip, pos);
    }

    public void EnemyEmerge2D()//******2d出现√
    {
    
    
        PlaySound2D(emergeClip);
    }

    public void Shock(Vector3 pos)
    {
    
    
        PlaySound(shockClip, pos);
    }

    public void PlayerDie(Vector3 pos)
    {
    
    
        PlaySound(playerDieClip, pos);
    }
    public void WinGame()
    {
    
    
        PlaySound2D(winClip);
    }
    #endregion


    #region UI/2D
    public void Click()
    {
    
    
        PlaySound2D(clickClip);
    }
    #endregion

    #endregion


    #region 竞速游戏第二个游戏音频区
    public void CoinTip(Vector3 pos)//3D音效,需要播放位置
    {
    
    
        PlaySound(coinTipClip, pos);
    }

    public void enemyCalling(Vector3 pos)//3D音效,怪兽叫
    {
    
    
        PlaySound(enemyCall, pos);
    }

    public void cubeAddClip(Vector3 pos)//3D音效,加1cube
    {
    
    
        PlaySound(cubeAdd, pos);
    }

    public void cubeDesClip(Vector3 pos)//3D音效,减1cube
    {
    
    
        PlaySound(cubeDesc, pos);
    }

    public void victoryPerson(Vector3 pos)//3D音效,勝利
    {
    
    
        PlaySound(SecondVictory, pos);
    }

    public void failedGame(Vector3 pos)//3D音效,失敗
    {
    
    
        PlaySound(failSound, pos);
    }


    #endregion

    #region 道具游戏
    public void NoProps()
    {
    
    
        PlaySound2D(noneClip);
    }
    public void PropsGameGetDamage(Vector3 pos)
    {
    
    
        PlaySound(propsGameGetDamageClip, pos);
    }

    public void StartOffMissile(Vector3 pos)
    {
    
    
        PlaySound(missileExplosionClip, pos);
    }

    public void MissileBomb(Vector3 pos)
    {
    
    
        PlaySound(missileExplosionClip, pos);
    }

    public void WoodBreak(Vector3 pos)
    {
    
    
        PlaySound(woodBreakClip, pos);
    }

    public void Cloak(Vector3 pos)
    {
    
    
        PlaySound(cloakingClip, pos);
    }

    public void PropsGameFlash(Vector3 pos)
    {
    
    
        PlaySound(flashClip, pos);
    }

    public void PropsGamePickUp()
    {
    
    
        PlaySound2D(pickingUpClip);
    }

    public void PropsGameAddHealth(Vector3 pos)
    {
    
    
        PlaySound(addHealthClip, pos);
    }

    public void PropGameSpeedUp(Vector3 pos)
    {
    
    
        PlaySound(upSpeedClip, pos);
    }

    public void SetLandmine(Vector3 pos)
    {
    
    
        PlaySound(setLandmineClip, pos);
    }
    #endregion
}

3. AudioMixer

The audio reverb in the project can be used to achieve overall control of the volume of various types of audio.
insert image description here

Guess you like

Origin blog.csdn.net/Fancy145/article/details/123950055