Unity - music, sound effects

During the running of the game, the timing of sound effects is closely related to the current content of the game, and as the scene changes and the plot progresses, the background music also needs to be switched in a timely manner, so it is very important to properly control the playback of music and sound effects. Playing, stopping, switching and volume changes of music and sound effects need to be controlled by scripts.


1. Use scripts to control music playback

Simply put, as long as there are Audio Source components and audio resources, music can be played. However, it is usually necessary to stop, switch music, etc. The following uses a sample script to illustrate how to play music. The steps are as follows

  1. Create a script TestAudio and hang it on any object
  2. Create an audio source object, the default object name is Audio Source

The script content is as follows 

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

public class TestAudio : MonoBehaviour
{
    //从外部指定声音片段
    public List<AudioClip> clips;
    //音源组件
    AudioSource audio;

    void Start()
    {
        //获取音源组件
        GameObject go = GameObject.Find("Audio Source");
        audio = go.GetComponent<AudioSource>();
        //先停止播放
        audio.Stop();
        //不循环
        audio.loop=false;
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Alpha1))
        {
            //切换到音乐0并播放
            audio.clip=clips[0];
            audio.Play();
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            //切换到音乐1并播放
            audio.clip = clips[1];
            audio.Play();
        }
        //按空格键 暂停/继续
        if (Input.GetKeyDown(KeyCode.Space))
        {
           if(audio.isPlaying)
            {
                audio.Pause();
            }
            else
            {
                audio.UnPause();
            }
        }
    }
}

 The script function is to play the first piece of audio according to the number, press to play the second piece, and press space to pause or continue playing

The script uses a public list field to hold audio resources. To assign an initial value to the variable clips.

The above demonstrates the common methods of the Audio Source component, and the properties of the audio source can be modified by scripts

2. Add sound effects

The approach to using music and sound effects in Unity is the same, but there are some key points to note.

First of all, one audio source can only play one audio at a time. In other words, if there are 10 sound effects playing at the same time, 10 sound sources are required. Therefore, each character in the game generally has an Audio Source component. For example, if the enemy shouts and the protagonist wields a weapon, then an Audio Source component is hung on each enemy and the protagonist. If the protagonist's weapon sound effects, jump sound effects, and injury sound effects may be played at the same time, then three Audio Source components can be hung on the protagonist, corresponding to one sound effect (of course, Audio Source components can also be placed on other objects).

Second, the difficulty of adding sound effects is the timing of the sound effects playing, it is not good for sound effects to play earlier or later than the character's actions. And according to the logic of the game, the same action sometimes needs to be accompanied by sound effects, and sometimes it does not, so it is also very important to figure out the conditions for playing sound effects.

Guess you like

Origin blog.csdn.net/m0_63024355/article/details/132528833