Unity horizontal 2D game learning example (08) - sound effect setting Audio & sound effect management

Preface: this section mainly adds the sound effect settings in the game

 

1, Download Audio Resources

The sound effects used in this section are free Audio resources of Unity store, as shown in the following two figures. After downloading and importing, put it in the Audio folder, and the preparation is completed.

 

2, How to use sound effects

1. When we want to add a sound effect to the character, such as jumping. Drag the jump sound effect to the Inspector window of the Player, and an Audio Source component will be automatically added.
(of course, you can add Audio Clip manually through add component - > audio - > audio source)

(note that this jump sound effect is played only when the character jumps, so it is necessary to remove play on wake in the figure: play when the game starts)


2. Get the component in the Player logic code and call the Play method in the jump part.

public class PlayerControl : MonoBehaviour
{
    public AudioSource jumpAudio;
    
    void Start()
    {
        jumpAudio = GetComponent<AudioSource>();
    }
    
    private void MidAirMovement()
    {  
        /**
        This is a jump logic code
        */
        
        jumpAudio.Play();
    }
}

Run the Game at this time, and there will be jumping sound effect when the character jumps. If there is no sound, you can pay attention to Mute Audio in the upper right corner of the Game window below

 

3, Sound management SoundManager

In the case of very small sound effects in the game, the above simple settings can meet the needs. However, if the game is a little more complicated, this setting method will hang various audio sources on characters, enemies, scenes, etc., which is chaotic, and it is not convenient for unified management in the menu later. So we need to establish a part that can uniformly manage sound effects. This part can be named SoundManager or AudioManager.

1. Create an empty object named SoundManager in the Hierarchy window.

2. Create a C# script SoundManager in the Scripts folder and mount it on the SoundManager object. (to facilitate management, you can create a folder with the name of System in Scripts to collect Manager Scripts)

3. Set the required audio field and playback method in the script

using UnityEngine;
using UnityEngine.Audio;

public class SoundManager : MonoBehaviour
{
    private static SoundManager instance;
    public static SoundManager Instance { get => instance; }

    [Header("Ambient sound")]
    [SerializeField]
    private AudioClip bgmAudio;

    [Header("Character sound")]
    [SerializeField]
    private AudioClip jumpAudio, hurtAudio;

    [Header("Collect sound effects")]
    [SerializeField]
    private AudioClip cherryAudio;

    [Header("Enemy sound")]
    [SerializeField]
    private AudioClip enemyDeathAudio;

    //Divide the sound source (play sound effect) for easy expansion
    private AudioSource bgmSource, playerSource, enemySource, fxSource;

    private void Awake()
    {
        if (instance == null)
            instance = this;
    }

    void Start()
    {
        //Initialize (add) sound source
        initSource();
        //Play background music at the beginning
        initBGM();
    }

    private void initBGM()
    {
        bgmSource.clip = bgmAudio;
        bgmSource.loop = true;
        bgmSource.Play();
    }

    private void initSource()
    {
        bgmSource = gameObject.AddComponent<AudioSource>();
        playerSource = gameObject.AddComponent<AudioSource>();
        enemySource = gameObject.AddComponent<AudioSource>();
        fxSource = gameObject.AddComponent<AudioSource>();
    }

    public void PlayJumpAudio()
    {
        playerSource.clip = jumpAudio;
        playerSource.Play();
    }

    public void PlayHurtAudio()
    {
        playerSource.clip = hurtAudio;
        playerSource.Play();
    }

    public void PlayCherryAudio()
    {
        fxSource.clip = cherryAudio;
        fxSource.Play();
    }

    public void PlayEnemyDeathAudio()
    {
        enemySource.clip = enemyDeathAudio;
        enemySource.Play();
    }
}

 4. Add the required audio (the following is the sound effect I think is appropriate. You can listen to the sound effect resource by yourself)

5. Call the PlayXXX method where necessary to play (see the previous chapter for "other codes")

public class PlayerControl : MonoBehaviour{

    private void MidAirMovement()
    {
        if (isJumpPressed && jumpCount > 0)
        {
            /**
            * Other codes
            */
            SoundManager.Instance.PlayJumpAudio();//Jump sound
        }
    }
    
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            if (transform.position.y > enemyObj.transform.position.y
                && player_Rbody.velocity.y < 0)
            {
                /**
                * Other codes
                */
            }
            else
            {
                /**
                * Other codes
                */

                //Injury sound
                SoundManager.Instance.PlayHurtAudio();
            }
        }
    }
}
public class Cheery : MonoBehaviour
{
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == playerLayerID)
        {
            /**
            * Other codes
            */
            SoundManager.Instance.PlayCherryAudio();//cherry is collecting sound effects
        }
    }
}
public class Enemy : MonoBehaviour
{
    public void Damaged()
    {
        /**
        * Other codes
        */
        SoundManager.Instance.PlayEnemyDeathAudio();//Enemy death sound
    }
}

At this point, you can hear the set sound effect by running the game again.

Added by EagleAmerican on Fri, 21 Jan 2022 22:50:46 +0200