Custom Audio Importer - only operate ONCE on new never-seen-before assets

I’ve been trying to use this script to set some custom import settings on my imported audio:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class AudioCustomImportSteps : AssetPostprocessor
{
    const float DefaultQualitySetting = 0.9f;

    void OnPreprocessAudio()
    {
        var importer = assetImporter as AudioImporter;

        AudioImporterSampleSettings aiss = importer.defaultSampleSettings;
        aiss.loadType = AudioClipLoadType.Streaming;
        aiss.quality = DefaultQualitySetting;
        importer.defaultSampleSettings = aiss;

        importer.preloadAudioData = false;
        importer.loadInBackground = false;

        Debug.Log( "AudioCustomImportSteps.OnPreprocessAudio(" + importer.name + ") - PRE!");
    }

    void OnPostprocessAudio(AudioClip clip)
    {
        Debug.Log( "AudioCustomImportSteps.OnPpstprocessAudio(" + clip.name + ") - POST!");
    }
}

This works great… but it works TOO well: I want it to only work once on WAV files that it has never seen before. When this script is present, it forces the affected settings ALWAYS to their values, no choice in the matter, and I cannot override them manually.

I want the above import step to only run on fresh audio samples it has never seen before. Do I have to keep track of what a first-time-import is myself and put that list under source control as well? Do I have to look at the timeStamp field and see if it is “oh just now or so” and make a choice?

Is this run-only-once-per-asset thing possible with Unity custom asset importers?

Thanks,
Kurt

That’s how I do it. I use AssetImporter.userData to mark whether an asset has been imported already.

1 Like

Ah, interesting solution! i didn’t even know that was possible. My simple solution would be to just check if the meta file already exists:

bool isNewFile = !File.Exists(assetPath + ".meta");
1 Like

@Peter77 and @Johannski , thank you both for great ideas. I had no idea about either option, now I have some new fun things to try out! Thanks!

Thanks for this. Works like a charm.