Batch audio clip import settings modifier

Hello,

I needed to modify the import settings of a shitload of audio clips in my project so I converted the nice editor script ChangeTextureImportSettings.cs (posted by Martin Schultz on the wiki) to work with audio clips and audio import settings. It adds a ‘Sound’ entry in the ‘Custom’ menu in the Unity editor and works exactly the same way.

I don’t have an account on the wiki and don’t want to bother to create one at the moment so here is ChangeAudioImportSettings.cs (to copy in the Assets/Editor/ folder):

using UnityEngine;
using UnityEditor;

// /////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Batch audio import settings modifier.
//
// Modifies all selected audio clips in the project window and applies the requested modification on the
// audio clips. Idea was to have the same choices for multiple files as you would have if you open the
// import settings of a single audio clip. Put this into Assets/Editor and once compiled by Unity you find
// the new functionality in Custom -> Sound. Enjoy! :-)
//
// April 2010. Based on Martin Schultz's texture import settings batch modifier.
//
// /////////////////////////////////////////////////////////////////////////////////////////////////////////
public class ChangeAudioImportSettings : ScriptableObject {

    [MenuItem ("Custom/Sound/Toggle audio compression/Disable")]
    static void ToggleCompression_Disable() {
        SelectedToggleCompressionSettings(AudioImporterFormat.Native);
    }

    [MenuItem ("Custom/Sound/Toggle audio compression/Enable")]
    static void ToggleCompression_Enable() {
        SelectedToggleCompressionSettings(AudioImporterFormat.Compressed);
    }

    // ----------------------------------------------------------------------------

    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/32")]
    static void SetCompressionBitrate_32kbps() {
        SelectedSetCompressionBitrate(32000);
    }

    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/64")]
    static void SetCompressionBitrate_64kbps() {
        SelectedSetCompressionBitrate(64000);
    }

    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/96")]
    static void SetCompressionBitrate_96kbps() {
        SelectedSetCompressionBitrate(96000);
    }

    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/128")]
    static void SetCompressionBitrate_128kbps() {
        SelectedSetCompressionBitrate(128000);
    }

    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/144")]
    static void SetCompressionBitrate_144kbps() {
        SelectedSetCompressionBitrate(144000);
    }

    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/156 (default)")]
    static void SetCompressionBitrate_156kbps() {
        SelectedSetCompressionBitrate(156000);
    }

    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/160")]
    static void SetCompressionBitrate_160kbps() {
        SelectedSetCompressionBitrate(160000);
    }

    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/192")]
    static void SetCompressionBitrate_192kbps() {
        SelectedSetCompressionBitrate(192000);
    }

    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/224")]
    static void SetCompressionBitrate_224kbps() {
        SelectedSetCompressionBitrate(224000);
    }

    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/240")]
    static void SetCompressionBitrate_240kbps() {
        SelectedSetCompressionBitrate(240000);
    }

    // ----------------------------------------------------------------------------

    [MenuItem ("Custom/Sound/Toggle decompress on load/Disable")]
    static void ToggleDecompressOnLoad_Disable() {
        SelectedToggleDecompressOnLoadSettings(false);
    }

    [MenuItem ("Custom/Sound/Toggle decompress on load/Enable")]
    static void ToggleDecompressOnLoad_Enable() {
        SelectedToggleDecompressOnLoadSettings(true);
    }

    // ----------------------------------------------------------------------------

    [MenuItem ("Custom/Sound/Toggle 3D sound/Disable")]
    static void Toggle3DSound_Disable() {
        SelectedToggle3DSoundSettings(false);
    }

    [MenuItem ("Custom/Sound/Toggle 3D sound/Enable")]
    static void Toggle3DSound_Enable() {
        SelectedToggle3DSoundSettings(true);
    }

    // ----------------------------------------------------------------------------

    [MenuItem ("Custom/Sound/Toggle mono/Auto")]
    static void ToggleForceToMono_Auto() {
        SelectedToggleForceToMonoSettings(false);
    }

    [MenuItem ("Custom/Sound/Toggle mono/Forced")]
    static void ToggleForceToMono_Forced() {
        SelectedToggleForceToMonoSettings(true);
    }

    // ----------------------------------------------------------------------------

    static void SelectedToggleCompressionSettings(AudioImporterFormat newFormat) {

        Object[] audioclips = GetSelectedAudioclips();
        Selection.objects = new Object[0];
        foreach (AudioClip audioclip in audioclips) {
            string path = AssetDatabase.GetAssetPath(audioclip);
            AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
            audioImporter.format = newFormat;
            AssetDatabase.ImportAsset(path);
        }
    }

    static void SelectedSetCompressionBitrate(float newCompressionBitrate) {

        Object[] audioclips = GetSelectedAudioclips();
        Selection.objects = new Object[0];
        foreach (AudioClip audioclip in audioclips) {
            string path = AssetDatabase.GetAssetPath(audioclip);
            AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
            audioImporter.compressionBitrate = newCompressionBitrate;
            AssetDatabase.ImportAsset(path);
        }
    }

    static void SelectedToggleDecompressOnLoadSettings(bool enabled) {

        Object[] audioclips = GetSelectedAudioclips();
        Selection.objects = new Object[0];
        foreach (AudioClip audioclip in audioclips) {
            string path = AssetDatabase.GetAssetPath(audioclip);
            AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
            audioImporter.decompressOnLoad = enabled;
            AssetDatabase.ImportAsset(path);
        }
    }

    static void SelectedToggle3DSoundSettings(bool enabled) {

        Object[] audioclips = GetSelectedAudioclips();
        Selection.objects = new Object[0];
        foreach (AudioClip audioclip in audioclips) {
            string path = AssetDatabase.GetAssetPath(audioclip);
            AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
            audioImporter.threeD = enabled;
            AssetDatabase.ImportAsset(path);
        }
    }

    static void SelectedToggleForceToMonoSettings(bool enabled) {

        Object[] audioclips = GetSelectedAudioclips();
        Selection.objects = new Object[0];
        foreach (AudioClip audioclip in audioclips) {
            string path = AssetDatabase.GetAssetPath(audioclip);
            AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
            audioImporter.forceToMono = enabled;
            AssetDatabase.ImportAsset(path);
        }
    }

    static Object[] GetSelectedAudioclips()
    {
        return Selection.GetFiltered(typeof(AudioClip), SelectionMode.DeepAssets);
    }
}

I tested it briefly on my project but please post any fix which may prove necessary.

Notice that Jashan already posted something similar here but I didn’t find it on my first searches.

3 Likes

Exactly what I needed right when I needed it. Thanks!

Same here, awesome! Was just about to create something myself.

That’s awesome thanks a lot.

thanks! I was about to make exactly this same thing but decided to see if anyone had beaten me to it. :slight_smile:

a few fixes:

I changed your “SelectedToggleDecompressOnLoadSettings” to:

static void SelectedToggleDecompressOnLoadSettings(AudioImporterLoadType loadType) {

Object[ ] audioclips = GetSelectedAudioclips();
Selection.objects = new Object[0];
foreach (AudioClip audioclip in audioclips) {
string path = AssetDatabase.GetAssetPath(audioclip);
AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
audioImporter.loadType = loadType;
AssetDatabase.ImportAsset(path);
}
}

and subsequently changed the corresponding menu items to:

[MenuItem (“Custom/Sound/Toggle decompress on load/Compressed in memory”)]
static void ToggleDecompressOnLoad_CompressedInMemory() {
SelectedToggleDecompressOnLoadSettings(AudioImporterLoadType.CompressedInMemory);
}

[MenuItem(“Custom/Sound/Toggle decompress on load/Decompress on load”)]
static void ToggleDecompressOnLoad_DecompressOnLoad() {
SelectedToggleDecompressOnLoadSettings(AudioImporterLoadType.DecompressOnLoad);
}

[MenuItem(“Custom/Sound/Toggle decompress on load/Stream from disc”)]
static void ToggleDecompressOnLoad_StreamFromDisc() {
SelectedToggleDecompressOnLoadSettings(AudioImporterLoadType.StreamFromDisc);
}

I also had to change “SelectedSetCompressionBitrate” to take an int rather than a float

so now it’s:

using UnityEngine;
using UnityEditor;
// /////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Batch audio import settings modifier.
//
// Modifies all selected audio clips in the project window and applies the requested modification on the
// audio clips. Idea was to have the same choices for multiple files as you would have if you open the
// import settings of a single audio clip. Put this into Assets/Editor and once compiled by Unity you find
// the new functionality in Custom -> Sound. Enjoy! :-)
//
// April 2010. Based on Martin Schultz's texture import settings batch modifier.
//
// /////////////////////////////////////////////////////////////////////////////////////////////////////////
public class ChangeAudioImportSettings : ScriptableObject {
 
    [MenuItem ("Custom/Sound/Toggle audio compression/Disable")]
    static void ToggleCompression_Disable() {
        SelectedToggleCompressionSettings(AudioImporterFormat.Native);
    }
 
    [MenuItem ("Custom/Sound/Toggle audio compression/Enable")]
    static void ToggleCompression_Enable() {
        SelectedToggleCompressionSettings(AudioImporterFormat.Compressed);
    }
 
    // ----------------------------------------------------------------------------
 
    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/32")]
    static void SetCompressionBitrate_32kbps() {
        SelectedSetCompressionBitrate(32000);
    }
 
    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/64")]
    static void SetCompressionBitrate_64kbps() {
        SelectedSetCompressionBitrate(64000);
    }
 
    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/96")]
    static void SetCompressionBitrate_96kbps() {
        SelectedSetCompressionBitrate(96000);
    }
 
    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/128")]
    static void SetCompressionBitrate_128kbps() {
        SelectedSetCompressionBitrate(128000);
    }
 
    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/144")]
    static void SetCompressionBitrate_144kbps() {
        SelectedSetCompressionBitrate(144000);
    }
 
    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/156 (default)")]
    static void SetCompressionBitrate_156kbps() {
        SelectedSetCompressionBitrate(156000);
    }
 
    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/160")]
    static void SetCompressionBitrate_160kbps() {
        SelectedSetCompressionBitrate(160000);
    }
 
    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/192")]
    static void SetCompressionBitrate_192kbps() {
        SelectedSetCompressionBitrate(192000);
    }
 
    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/224")]
    static void SetCompressionBitrate_224kbps() {
        SelectedSetCompressionBitrate(224000);
    }
 
    [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/240")]
    static void SetCompressionBitrate_240kbps() {
        SelectedSetCompressionBitrate(240000);
    }
 
    // ----------------------------------------------------------------------------
 
    [MenuItem ("Custom/Sound/Toggle decompress on load/Compressed in memory")]
    static void ToggleDecompressOnLoad_CompressedInMemory() {
        SelectedToggleDecompressOnLoadSettings(AudioImporterLoadType.CompressedInMemory);
    }

    [MenuItem("Custom/Sound/Toggle decompress on load/Decompress on load")]
    static void ToggleDecompressOnLoad_DecompressOnLoad() {
        SelectedToggleDecompressOnLoadSettings(AudioImporterLoadType.DecompressOnLoad);
    }

    [MenuItem("Custom/Sound/Toggle decompress on load/Stream from disc")]
    static void ToggleDecompressOnLoad_StreamFromDisc() {
        SelectedToggleDecompressOnLoadSettings(AudioImporterLoadType.StreamFromDisc);
    }

    // ----------------------------------------------------------------------------
 
    [MenuItem ("Custom/Sound/Toggle 3D sound/Disable")]
    static void Toggle3DSound_Disable() {
        SelectedToggle3DSoundSettings(false);
    }
 
    [MenuItem ("Custom/Sound/Toggle 3D sound/Enable")]
    static void Toggle3DSound_Enable() {
        SelectedToggle3DSoundSettings(true);
    }
 
    // ----------------------------------------------------------------------------
 
    [MenuItem ("Custom/Sound/Toggle mono/Auto")]
    static void ToggleForceToMono_Auto() {
        SelectedToggleForceToMonoSettings(false);
    }
 
    [MenuItem ("Custom/Sound/Toggle mono/Forced")]
    static void ToggleForceToMono_Forced() {
        SelectedToggleForceToMonoSettings(true);
    }
 
    // ----------------------------------------------------------------------------
 
    static void SelectedToggleCompressionSettings(AudioImporterFormat newFormat) {
 
        Object[] audioclips = GetSelectedAudioclips();
        Selection.objects = new Object[0];
        foreach (AudioClip audioclip in audioclips) {
            string path = AssetDatabase.GetAssetPath(audioclip);
            AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
            audioImporter.format = newFormat;
            AssetDatabase.ImportAsset(path);
        }
    }
 
    static void SelectedSetCompressionBitrate(int newCompressionBitrate) {
 
        Object[] audioclips = GetSelectedAudioclips();
        Selection.objects = new Object[0];
        foreach (AudioClip audioclip in audioclips) {
            string path = AssetDatabase.GetAssetPath(audioclip);
            AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
            audioImporter.compressionBitrate = newCompressionBitrate;
            AssetDatabase.ImportAsset(path);
        }
    }
 
    static void SelectedToggleDecompressOnLoadSettings(AudioImporterLoadType loadType) {
 
        Object[] audioclips = GetSelectedAudioclips();
        Selection.objects = new Object[0];
        foreach (AudioClip audioclip in audioclips) {
            string path = AssetDatabase.GetAssetPath(audioclip);
            AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
            audioImporter.loadType = loadType;
            AssetDatabase.ImportAsset(path);
        }
    }
 
    static void SelectedToggle3DSoundSettings(bool enabled) {
 
        Object[] audioclips = GetSelectedAudioclips();
        Selection.objects = new Object[0];
        foreach (AudioClip audioclip in audioclips) {
            string path = AssetDatabase.GetAssetPath(audioclip);
            AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
            audioImporter.threeD = enabled;
            AssetDatabase.ImportAsset(path);
        }
    }
 
    static void SelectedToggleForceToMonoSettings(bool enabled) {
 
        Object[] audioclips = GetSelectedAudioclips();
        Selection.objects = new Object[0];
        foreach (AudioClip audioclip in audioclips) {
            string path = AssetDatabase.GetAssetPath(audioclip);
            AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
            audioImporter.forceToMono = enabled;
            AssetDatabase.ImportAsset(path);
        }
    }
 
    static Object[] GetSelectedAudioclips()
    {
        return Selection.GetFiltered(typeof(AudioClip), SelectionMode.DeepAssets);
    }
}
2 Likes

Thanks for the awesome script, just what I need.

I think this is a silly way to do it. Here’s mine:

using UnityEditor;
using UnityEngine;

public class SetAudioSettings : EditorWindow {

	private AudioImporterFormat format;
	bool threeD, mono, hardware, gapless;
	private AudioImporterLoadType loadType;
	private int compression = 196;

	[MenuItem("Tools/Bulk Audio Settings")]
	private static void Init() {
		GetWindow(typeof(SetAudioSettings));
	}
	void OnGUI() {
		format = (AudioImporterFormat)EditorGUILayout.EnumPopup("Audio Format", format);
		threeD = EditorGUILayout.Toggle("3D sound", threeD);
		mono = EditorGUILayout.Toggle("Force to mono", mono);
		loadType = (AudioImporterLoadType)EditorGUILayout.EnumPopup("Load Type", loadType);
		hardware = EditorGUILayout.Toggle("Hardware decoding", hardware);
		gapless = EditorGUILayout.Toggle("Gapless Looping", gapless);
		compression = EditorGUILayout.IntSlider("Compression (kbps)", compression, 0, 256);

		if (GUILayout.Button("Set Settings")) {
			foreach (Object o in Selection.GetFiltered(typeof(AudioClip), SelectionMode.DeepAssets)) {
				string path = AssetDatabase.GetAssetPath(o);
				AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
				audioImporter.format = format;
				audioImporter.threeD = threeD;
				audioImporter.forceToMono = mono;
				audioImporter.loadType = loadType;
				audioImporter.hardware = hardware;
				audioImporter.compressionBitrate = compression * 1000;
				AssetDatabase.ImportAsset(path);
			}
		}
	}
}
2 Likes

Thanks guys this saved me a couple of hours :slight_smile:

Note,

Around 2015 Unity finally added multiple selection of AudioClip -

so fortunately this problem no longer exists.

what about changing the Load Type through code? i would still prefer to have the option to do this through an editor script.