Optimize Audio Importer settings by sound duration
Importing multi-minute music tracks with Decompress On Load unpacks uncompressed PCM data directly into RAM, wasting tens of megabytes. Use Vorbis with Compressed In Memory for long background tracks and uncompressed PCM only for latency-critical sound effects.
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
public class AudioImportOptimizer : AssetPostprocessor
{
private void OnPreprocessAudio()
{
AudioImporter importer = (AudioImporter)assetImporter;
AudioImporterSampleSettings settings = importer.defaultSampleSettings;
if (assetPath.Contains("/BGM/"))
{
importer.loadInBackground = true;
settings.loadType = AudioClipLoadType.CompressedInMemory;
settings.compressionFormat = AudioCompressionFormat.Vorbis;
settings.quality = 0.7f;
}
else if (assetPath.Contains("/SFX/"))
{
settings.loadType = AudioClipLoadType.DecompressOnLoad;
settings.compressionFormat = AudioCompressionFormat.PCM;
}
importer.defaultSampleSettings = settings;
}
}
#endifLogarithmic decibel scaling for mixer volume sliders
AudioMixer attenuation parameters operate in decibels (-80dB to 0dB). Directly passing linear UI slider values (0.0 to 1.0) creates an unnatural drop-off. Convert linear values using Mathf.Log10 to match human hearing perception.
using UnityEngine;
using UnityEngine.Audio;
public class SoundVolumeController : MonoBehaviour
{
[SerializeField] private AudioMixer mainMixer;
public void SetMasterVolume(float linearSliderValue)
{
float clamped = Mathf.Clamp(linearSliderValue, 0.0001f, 1f);
float dB = Mathf.Log10(clamped) * 20f;
mainMixer.SetFloat("MasterVolume", dB);
}
public void SetBgmVolume(float linearSliderValue)
{
float clamped = Mathf.Clamp(linearSliderValue, 0.0001f, 1f);
float dB = Mathf.Log10(clamped) * 20f;
mainMixer.SetFloat("BGMVolume", dB);
}
}Sidechain Ducking for dialogue and key FX
Add a Send effect on the SFX/Voice mixer group and a Duck Volume effect on the BGM group. The mixer will automatically attenuate background music whenever active sound effects fire.
Official documentation review
Verified
Reviewed the article's code and procedure against official Unity documentation and marked it ready for publication. This is not a claim of an independent Unity project or device reproduction; version- or device-specific reports will be checked in that environment as follow-up.