Krotox Engine › Docs › Scripting API › Graphics, UI & Audio

Audio API

Playing sound from C#: Audio Source components on entities, fire-and-forget sounds and music with AudioCommand, fades and cross-fades, mixer groups and volume settings, muting, and spatial sound.

There are two ways to play sound:

Paths are relative to the Assets folder. Supported formats: .wav, .mp3, .ogg, .flac.

Audio Source on an entity

var source = GetComponent<AudioComponent>();

source.Clip = "Audio/engine_loop.ogg";   // takes effect on the next Play()
source.Loop = true;
source.Group = AudioGroup.SFX;
source.Play();                            // restarts if already playing

source.Volume = 0.6f;                     // Volume, Pitch, Pan and Loop change a playing sound at once
source.Pitch = 1.2f;
source.Pause();
source.Resume();

source.FadeOutDuration = 0.5f;
source.Stop();                            // fades out when FadeOutDuration > 0
bool playing = source.IsPlaying;

Other members: PlayOnStart, Pan, FadeInDuration.

Spatial sound

source.Spatial = true;                    // quieter with distance from the listener
source.MinDistance = 2.0f;                // full volume inside this distance
source.MaxDistance = 30.0f;               // silent beyond this

GetComponent<AudioListenerComponent>().Active = true;   // usually on the camera or the player

Engine pitch from speed

void OnUpdate(float ts)
{
    float speed = GetComponent<Rigidbody2DComponent>().LinearVelocity.Length();
    m_Engine.Pitch = 0.8f + Mathf.Clamp01(speed / 20.0f) * 0.8f;
}

AudioCommand: one-off sounds and music

AudioCommand.PlaySound2D("Audio/coin.wav");                  // fire and forget
AudioCommand.Preload("Audio/explosion.wav");                 // decode ahead of time to avoid a hitch

// Calls that return a handle let you control the sound afterwards
ulong music = AudioCommand.Play("Audio/theme.ogg", AudioGroup.Music, volume: 0.8f, loop: true);
AudioCommand.SetVolume(music, 0.5f);
AudioCommand.FadeOutAndStop(music, 1.5f);

// Switch tracks smoothly
ulong boss = AudioCommand.CrossFade(music, "Audio/boss.ogg", 2.0f);

Handle functions: Stop, Pause, Resume, IsPlaying, SetVolume, SetPitch, FadeVolume, FadeOutAndStop, GetDuration, GetPlaybackPosition, SetPlaybackPosition.

Mixer groups and settings

Sounds play through mixer groups: Master, Music, SFX, UI, Dialogue and Ambience. Balance them in Window ▸ Audio Mixer, and expose them in your settings menu:

AudioCommand.MasterVolume = 0.85f;
AudioCommand.SetGroupVolume(AudioGroup.Music, m_MusicSlider.Value);
AudioCommand.SetGroupVolume(AudioGroup.SFX, m_SfxSlider.Value);
AudioCommand.SavePreferences();          // remember them for next time

// Muting
AudioCommand.ToggleMasterMute();
bool muted = AudioCommand.MasterMuted;
AudioCommand.SetGroupMuted(AudioGroup.Music, true);
bool musicMuted = AudioCommand.IsGroupMuted(AudioGroup.Music);
AudioCommand.ToggleGroupMute(AudioGroup.SFX);

Music that survives scene loads

Put the music on an entity that is kept across scenes (see keeping entities across scenes), or simply keep the handle from AudioCommand.Play in a static field:

public class Music : Entity
{
    static ulong s_Track;

    void OnCreate()
    {
        if (s_Track == 0 || !AudioCommand.IsPlaying(s_Track))
            s_Track = AudioCommand.Play("Audio/theme.ogg", AudioGroup.Music, volume: 0.7f, loop: true);
    }
}

Mobile tips