To add background sound in Unity, you'll primarily use the AudioSource component attached to a GameObject. Here's a breakdown of the process:
1. Import Your Audio File:
- Method 1: Drag and Drop: Drag your audio file (e.g., .mp3, .wav, .ogg) directly from your computer's file system into the Unity Editor's Project panel.
- Method 2: Import New Asset: Right-click within the Project panel, select
Import New Asset
, and then browse to and select your audio file.
2. Create a GameObject for the Sound:
It's good practice to create a dedicated GameObject to manage your background music.
- In the Hierarchy panel (Window -> General -> Hierarchy), right-click and select
Create Empty
. Name it something descriptive, like "BackgroundMusic".
3. Add an AudioSource Component:
- Select the "BackgroundMusic" GameObject you just created.
- In the Inspector panel (Window -> General -> Inspector), click
Add Component
. - Search for "AudioSource" and select it to add the component to your GameObject.
4. Configure the AudioSource Component:
- Audio Clip: In the AudioSource component's Inspector panel, find the "Audio Clip" field. Drag your imported audio file from the Project panel into this field.
- Play On Awake: Check this box if you want the music to start playing automatically when the scene loads. If you want more control, leave it unchecked and use scripting.
- Loop: Check this box if you want the music to loop continuously. This is generally desirable for background music.
- Volume: Adjust the "Volume" slider to set the desired loudness of the music (0.0 to 1.0).
- Spatial Blend: Set this to "2D" for background music that shouldn't have positional audio effects. 3D sound can be useful if the source of the music should appear to be coming from a particular location.
5. Scripting (Optional, for More Control):
If you want more control over when the music starts and stops, use a script. Here's a simple C# script to play the background music when a scene starts:
using UnityEngine;
public class BackgroundMusic : MonoBehaviour
{
private AudioSource audioSource;
void Start()
{
audioSource = GetComponent<AudioSource>();
audioSource.Play(); // Start playing the music
}
}
- Create a new C# script (e.g., "BackgroundMusic.cs") in your Project panel.
- Copy and paste the code above into the script.
- Attach the script to your "BackgroundMusic" GameObject. (Drag the script from the Project panel onto the GameObject in the Hierarchy panel).
- Make sure "Play On Awake" is unchecked in the AudioSource component, as the script will now handle starting the music.
You can also use scripting to:
- Stop the music:
audioSource.Stop();
- Pause the music:
audioSource.Pause();
- Resume the music:
audioSource.UnPause();
- Fade the music in and out.
- Control the music based on in-game events.
Example:
Let's say you have an audio file named "background_music.mp3". You import it, create a "BackgroundMusic" GameObject, add an AudioSource, drag "background_music.mp3" to the Audio Clip field, check "Loop", and adjust the volume. Now, when you run the scene, the music will play continuously in the background.