HOME/Articles/

AudioImporter as Editor Extension

Article Outline

AssetPostprocessor

If there are the same tasks every time you add a file to a Unity project, they should be automated. The task can be automated by hooking an event on import.

For example,

using UnityEditor;

public class MyPostprocessor : AssetPostprocessor
{
    private void OnPreprocessAsset()
    {
        // Add this function to a subclass to get a notification just before any Asset is imported.
        ...
    }
}

this method is called before any Asset is imported. See OnPreprocessAsset for details.

OnPreprocessAudio

Use OnPostprocessAudio to hook audio imports. In the following sample, forceToMono for audio imported to the SE folder is set to true, and loadInBackground for audio imported to the BGM folder is set to true.

using UnityEditor;

public class AudioProcessing : AssetPostprocessor
{
    private void OnPreprocessAudio()
    {
        var importer = assetImporter as AudioImporter;
        var path = assetImporter.assetPath;

        importer.forceToMono = path.Contains("SE");
        importer.loadInBackground = path.Contains("BGM");
    }
}

Usages

Importer

BGM

SE

Samples

See the source code here, unity-audio-importer.