Play multiple sounds at the same time
Verification environment
- Windows
-
- Windows 11
- Unity Editor
-
- 2021.3.3f1
- Input System Package
-
- 1.3.0
Prerequisites for this tip
The following settings have been made in advance as a premise for the description of this tip.
About the material included with the sample
Sound effects are borrowed from the following sites.
About audio files
The following audio file formats can be played with Unity's standard features: Please prepare it in advance because it will be used in these tips.
- WAV (.wav)
- OggVorbis (.ogg)
- MPEG layer 3 (.mp3)
For more information, see the official Unity documentation.
About multiple simultaneous audio playback
In the previous tips, we used AudioSource to play an audio file, but when we tried to make a single sound and multiple sounds, the previous sound disappeared. For example, if you have two explosions in a game, it is unnatural for the first sound effect to disappear when the second explosion generates a sound effect. This section describes how to play the same sound multiple times on top of each other.
Steps for playing audio at the same time
Here, I will explain from the point where I created a new project, but if you know Unity to a certain extent, you can cut out only the relevant parts and memorize them.
This time, the sound effect will play every time you click the button. Create your UI as shown. The details are appropriate and good.
Add a sound file by dropping it into your project.
Add Audio Source to the hierarchy. In this case, you won't set the audio clip directly to the AudioSource.
Add a script. ButtonEvent
Leave the name as .
The script looks like this:
using UnityEngine;
public class ButtonEvent : MonoBehaviour
{
[SerializeField] private AudioSource AudioSource;
<summary>音声データ。</summary>
[SerializeField] private AudioClip AudioClip;
public void OnClickPlay()
{
// オーディオを再生します
AudioSource.PlayOneShot(AudioClip);
}
}
AudioSource
Think of it as a class for controlling audio, just like the previous tips.
AudioClip
is a class that stores audio data. You can set audio files here.
AudioSource.PlayOneShot
If you pass it as an argument to the method, you can play the AudioClip
audio data.
The audio played by this method is controlled independently.
If you call this method again, it will be played as a new voice separately from the previous one.
Therefore, it is possible to play the sound again every time this method is called.
Attach the script to EventSystem.
For "Audio Source", set the "Audio Source" added to the hierarchy.
For "Audio clip", set an audio file from the project.
Finally, set the method to the button's click event.
Run the game and click the button. I think the sound effects will play. Also, if you click the button again during playback, the new sound will be played without the previous sound disappearing.
AudioClip
In this way, it is now possible to play the same sound on top of each other by playing the audio via .
This allows the game to play without the previous sound disappearing unnaturally.
Disadvantages when playing with AudioClip
AudioClip
I was able to play the audio on top of each other by playing it using , but on the contrary, the following disadvantages occur.
The sound becomes noisy
As you can see if you click the button continuously, the sounds overlap and the harmonic that is played becomes louder and louder. For example, if you want to play a sound effect when an attack hits in a game, If multiple hits occur at the same time without taking any measures, they may be played at an abnormal volume.