Audio Core API

This section delves into the core audio functionalities provided by the .NET Gaming framework. Effective audio management is crucial for creating immersive and engaging gaming experiences. This API allows developers to load, play, and manipulate sound effects, background music, and spatial audio.

Key Concepts

Working with Audio Clips

Audio clips are the fundamental units of sound. They can be loaded from various sources:


// Loading an audio clip from a file
AudioClip backgroundMusic = AudioManager.LoadAudioClip("Assets/Music/main_theme.ogg");
AudioClip jumpSound = AudioManager.LoadAudioClip("Assets/SFX/jump.wav");
        

Audio Sources

Audio Sources are attached to game objects and are responsible for playing audio clips. You can configure properties like looping, spatial blending, and playback state.

Creating and Configuring an Audio Source

You can add an AudioSource component to a game object in your scene:


GameObject playerObject = new GameObject("Player");
AudioSource playerAudio = playerObject.AddComponent();

// Assign an audio clip to the source
playerAudio.Clip = jumpSound;
playerAudio.Volume = 0.8f;
playerAudio.Loop = false;
playerAudio.PlayOnAwake = false;
        

Playing Audio

Audio can be played programmatically:


// Play a sound once
playerAudio.Play();

// Play background music
AudioSource musicSource = new AudioSource();
musicSource.Clip = backgroundMusic;
musicSource.Loop = true;
musicSource.Volume = 0.5f;
musicSource.Play();

// Stop playback
musicSource.Stop();
        

Spatial Audio

Spatial audio allows sounds to originate from specific positions in 3D space, creating a more realistic listening experience. This is achieved by combining AudioSource and AudioListener components.

Audio Listener

Every scene typically has one AudioListener, usually attached to the main camera or the player character.


// Typically attached to the camera
Camera mainCamera = Camera.main;
AudioListener listener = mainCamera.GetComponent();
if (listener == null) {
    listener = mainCamera.gameObject.AddComponent();
}
        

3D Sound Settings

AudioSource components have properties to control how sound attenuates with distance and spreads in 3D space.

Property Description
SpatialBlend Determines how much the audio source is affected by 3D spatialization. 0 is full 2D, 1 is full 3D.
MinDistance The distance at which the audio source reaches its maximum volume.
MaxDistance The distance at which the audio source's volume fades to zero.

Core Classes

Note on Audio Formats

The .NET Gaming framework supports common audio formats like WAV, MP3, and OGG Vorbis. Ensure your audio assets are in a compatible format for best results.

Performance Tip

For long-playing background music or ambient sounds, consider using streaming audio clips to reduce memory overhead. For short sound effects, loading them fully into memory is generally more performant.

Audio Mixer Groups

Audio mixers provide a powerful way to control and mix audio streams. You can create groups for music, sound effects, UI sounds, etc., and control their volumes independently.

Example: Setting up Master and SFX Volume


// Assuming you have mixer groups named "Master" and "SFX"
MixerGroup masterGroup = AudioManager.GetMixerGroup("Master");
MixerGroup sfxGroup = AudioManager.GetMixerGroup("SFX");

AudioSource effectSource = someGameObject.AddComponent();
effectSource.Clip = jumpSound;
effectSource.OutputGroup = sfxGroup; // Assign to SFX group
effectSource.Play();

// Adjust master volume
AudioManager.SetVolume(masterGroup, 0.7f);

// Adjust SFX volume
AudioManager.SetVolume(sfxGroup, 1.0f);
        

Advanced Features

Explore the Advanced Audio Effects section for more details on these features.