How to play sounds in Unity?
First we have to understand how Sound Effects Work in Unity.
To play sounds in Unity you need an Audio Source and an Audio Clip .
An Audio Source is the component that will actually play the sound in 2D or 3D space or as a mixture (SpatialBlend). In 3D space, the sound’s volume can vary based on the distance between the Audio Source and the object listening to it: the Audio Listener, which is usually attached to the main camera or to the gameObject that represents the player.
The Audio Listener acts as a microphone-like device. It receives input from an Audio Source in the scene and plays sounds through the computer speakers. Each scene can only have one Audio Listener to work properly.
In 2D space, however, the Audio Source component will ignore any 3D processing and the sound will play at a consistent volume regardless of the distance from the Audio Listener.
An Audio Clip is the actual audio file that the Audio Source will play. The audio file formats that Unity can import are .wav, .mp3, .ogg and .aif.
To create a new Audio Source:
- Copy and paste your audio files into your Unity Project. These are now Audio Clips;
- Add an Audio Source component to an empty gameObject or any other gameObject in your scene;
- In the Inspector, find the Audio Clip property on the Audio Source component and assign a clip, either by dragging one from the Project Window or by clicking the small circle icon to the right of the Inspector property, then selecting a clip from the list.
You can also drag an Audio clip from the Project window to the Scene view or the Hierarchy window. A new gameObject with an Audio Source component will be created automatically for it.
Now you can choose which options you want to enable or disable in the Audio Source component. For example, you can enable the “Play On Awake” option to play the sound automatically when the gameObject is created. Check the “Loop” checkbox to repeat the sound continuously. These two options are appropriate for background sound. If, instead, you want to play a bullet shot sound when clicking on a button using a script, then you may have to disable these options.
How to play a sound using a script in Unity?
To play a sound using a script, attach the following code example to the gameObject containing the Audio Source Component. This code will play the audio clip of this Audio Source at the first execution of the script (in the Start function). In addition, it will create two buttons, one for pausing the sound and the other for unpausing it.
using UnityEngine;public class PlaySound : MonoBehaviour
AudioSource MyAudio;void Start()
//Get a reference to the Audio Source Component
MyAudio = GetComponent<AudioSource>();
//Play the Audio Clip at start
}// This function is called for rendering and handling GUI (Graphic User Interface) events.
//Create a pause button
if (GUI.Button(new Rect(10, 70, 150, 30), "Pause"))
//Pause the Audio Clip
//Create an unpause button
if (GUI.Button(new Rect(10, 170, 150, 30), "Continue"))
//Unpausing the Audio Clip
In addition, you can access the Project global Audio settings using the Audio category in the Project Settings (main menu: Edit => Project Settings => Audio category).