CodeBlanks Logo Icon CodeBlanks

Importing an AudioClip

Learn to import .wav and .mp3 files into the project.

Importing AudioClip and audio formats

Sound files imported into Unity projects automatically convert into AudioClip assets. Because .wav files are uncompressed and lossless, they are optimal for short sound effects (SFX) like jumps or gunfire.

// Loading an AudioClip from resources
AudioClip laserClip = Resources.Load<AudioClip>("Audio/Laser");

Meanwhile, .mp3 and .ogg formats leverage compression, making them ideal for long background soundtracks.

Load Type and memory optimization

The Load Type setting in the audio Inspector determines how clips reside in RAM. Short sound effects typically use Decompress On Load.

// Accessing clip length in seconds
float clipDuration = laserClip.length;

For multi-minute musical tracks, selecting Streaming conserves RAM by buffering the audio in chunks directly from disk.

Practice Print format and load mode

Assign "WAV" to format and "DecompressOnLoad" to loadType. Print Format: WAV | LoadType: DecompressOnLoad to the console.

Starter code

Player.cs
using System;

class Program
{
    static void Main()
    {
        // format ve loadType değerlerini tanımlayıp yazdır
    }
}

Sample solution

Player.cs
using System;

class Program
{
    static void Main()
    {
        string format = "WAV";
        string loadType = "DecompressOnLoad";
        Console.WriteLine("Format: " + format + " | LoadType: " + loadType);
    }
}

Expected output

Format: WAV | LoadType: DecompressOnLoad

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which file format is most suitable for instantaneous and short sound effects (SFX) like gunshots or jumps?

  • WAV Correct answer
  • PNG
  • TXT
  • MP4

Explanation: WAV is uncompressed, making it the ideal sound effect format that plays instantaneously without CPU decompression overhead.

2 Load audio of AudioClip type

Question
AudioClip clip = Resources.Load<____>("Audio/Music");
Answer
AudioClip clip = Resources.Load<AudioClip>("Audio/Music");

Explanation: When loading audio assets via generic Resources.Load in Unity, the AudioClip type is specified.

3 Audio import concepts

Term Meaning
WAV Uncompressed format for short sounds
MP3 Compressed format for long music
Streaming Load mode reading audio in chunks
AudioClip Base Unity object for imported audio

All lessons in Intermediate