- Home /
Set Import Setting When an Audio Clip is Dragged into the Project
I'm trying to use the OnPreprocessAudio callback to override the clip's import settings when it is first dragged into the project (not every time the import settings change).
To do this, I try to load the asset, and if it doesn't exist, then I apply the default settings.
void OnPreprocessAudio()
{
if (DefaultImportSettingsWindow.applyOnImport) //my own boolean
{
try
{
Object asset = AssetDatabase.LoadAssetAtPath(assetPath, typeof(AudioClip));
if (asset == null && DefaultImportSettingsWindow.audioDefault != null)
{
AudioImporter defaultAudioImporter = (AudioImporter)AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(DefaultImportSettingsWindow.audioDefault));
//apply defaults here
}
}
catch (System.Exception e)
{
Debug.Log("Default Import Settings had this error while importing an audio clip: " + e.Message);
}
}
}
This successful sets my defaults. However, I sometimes get the following error in the console:
Unhandled load type UnityEditor.DockArea:OnGUI()
This occurs when I drag a new audio clip in, or when I change the import settings of an audio clip manually in the inspector. And it isn't caught by my try/catch block. Stepping into the function in the debugger, I see that the exception is thrown after OnPreprocessAudio() is called. The problem is fixed if I remove the call to AssetDatabase.LoadAssetAtPath, but I need this to determine if it is a new audio clip. I've been looking for alternative ways to detect if the audio is new: both AssetDatabase.LoadAllAssetsAtPath and AssetDatabase.FindAssets give the same problem.
Strangely, I wrote analogous code for model and texture imports. Neither have this problem.