Skip to content

Instantly share code, notes, and snippets.

@Minimally
Created May 14, 2014 19:32
Show Gist options
  • Save Minimally/b68f34524a74a2b43162 to your computer and use it in GitHub Desktop.
Save Minimally/b68f34524a74a2b43162 to your computer and use it in GitHub Desktop.
Unity Editor script to customize importing audio with various configurations.
using UnityEditor;
using UnityEngine;
/// <summary>
/// Modify the way audio is imported into Unity
/// Throw this script in the Editor folder.
/// Place audio files in folders named with specific keywords listed below to automate how they are imported.
/// Check Unity reference for details and such
/// </summary>
public class CBAudioImport : AssetPostprocessor
{
const string forceToMonoKeyword = "mono";
const string iosHardwareDecoding = "ios";
/* The method Unity uses to load audio assets at runtime.
* Audio files will be decompressed as soon as they are loaded.
* Use this option for smaller compressed sounds to avoid the performance overhead of decompressing on the fly.
* Be aware that decompressing sounds on load will use about ten times more memory than keeping them compressed, so don't use this option for large files. */
const string sfxKeyword = "sfx";
/* Keep sounds compressed in memory and decompress while playing.
* This option has a slight performance overhead (especially for Ogg/Vorbis compressed files) so only use it for bigger files
* where decompression on load would use a prohibitive amount of memory.
* Note that, due to technical limitations, this option will silently switch to Stream From Disc for Ogg Vorbis assets on platforms that use FMOD audio. */
const string sfxLargeKeyword = "large";
/* Stream audio data directly from disc.
* The memory used by this option is typically a small fraction of the file size, so it is very useful for music or other very long tracks.
* For performance reasons, it is usually advisable to stream only one or two files from disc at a time but
* the number of streams that can comfortably be handled depends on the hardware. */
const string musicKeyword = "music";
const string loopKeyword = "loop";
const string threeD = "3d";
/// <summary> Audio importer lets you modify AudioClip import settings using keyword named folders. </summary>
public void OnPreprocessAudio()
{
AudioImporter audioImporter = (AudioImporter)assetImporter;
// Compression bitrate in bits/second, e.g. 128000 would be 128 kbps. - Native or Ogg compressed.
Debug.Log(string.Format("Importing:{0} - {1} bits/second - {2}", assetPath, audioImporter.compressionBitrate, audioImporter.format.ToString()));
// Force this clip to mono?
if (assetPath.Contains(forceToMonoKeyword))
audioImporter.forceToMono = true;
// Use hardware voice/decoder (Apple hardware codec) on iOS.
if (assetPath.Contains(iosHardwareDecoding))
audioImporter.hardware = true;
// The method Unity uses to load audio assets at runtime.
if (assetPath.Contains(sfxKeyword))
audioImporter.loadType = AudioImporterLoadType.DecompressOnLoad;
else if (assetPath.Contains(sfxLargeKeyword))
audioImporter.loadType = AudioImporterLoadType.CompressedInMemory;
else if (assetPath.Contains(musicKeyword))
audioImporter.loadType = AudioImporterLoadType.StreamFromDisc;
// Is this clip loopable?
audioImporter.loopable = assetPath.Contains(loopKeyword);
// Is this clip a 2D or 3D sound?
audioImporter.threeD = assetPath.Contains(threeD);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment