1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice
  3. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  4. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  5. Unity 5.5 is now released.
    Dismiss Notice
  6. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  7. Unity 5.6 beta is now available for download.
    Dismiss Notice

Batch audio clip import settings modifier

Discussion in 'Scripting' started by Mat, Apr 15, 2010.

  1. Mat

    Mat

    Joined:
    Dec 4, 2009
    Posts:
    30
    Hello,

    I needed to modify the import settings of a S***load 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):
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. // /////////////////////////////////////////////////////////////////////////////////////////////////////////
    6. //
    7. // Batch audio import settings modifier.
    8. //
    9. // Modifies all selected audio clips in the project window and applies the requested modification on the
    10. // audio clips. Idea was to have the same choices for multiple files as you would have if you open the
    11. // import settings of a single audio clip. Put this into Assets/Editor and once compiled by Unity you find
    12. // the new functionality in Custom -> Sound. Enjoy! :-)
    13. //
    14. // April 2010. Based on Martin Schultz's texture import settings batch modifier.
    15. //
    16. // /////////////////////////////////////////////////////////////////////////////////////////////////////////
    17. public class ChangeAudioImportSettings : ScriptableObject {
    18.  
    19.     [MenuItem ("Custom/Sound/Toggle audio compression/Disable")]
    20.     static void ToggleCompression_Disable() {
    21.         SelectedToggleCompressionSettings(AudioImporterFormat.Native);
    22.     }
    23.  
    24.     [MenuItem ("Custom/Sound/Toggle audio compression/Enable")]
    25.     static void ToggleCompression_Enable() {
    26.         SelectedToggleCompressionSettings(AudioImporterFormat.Compressed);
    27.     }
    28.  
    29.     // ----------------------------------------------------------------------------
    30.  
    31.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/32")]
    32.     static void SetCompressionBitrate_32kbps() {
    33.         SelectedSetCompressionBitrate(32000);
    34.     }
    35.  
    36.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/64")]
    37.     static void SetCompressionBitrate_64kbps() {
    38.         SelectedSetCompressionBitrate(64000);
    39.     }
    40.  
    41.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/96")]
    42.     static void SetCompressionBitrate_96kbps() {
    43.         SelectedSetCompressionBitrate(96000);
    44.     }
    45.  
    46.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/128")]
    47.     static void SetCompressionBitrate_128kbps() {
    48.         SelectedSetCompressionBitrate(128000);
    49.     }
    50.  
    51.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/144")]
    52.     static void SetCompressionBitrate_144kbps() {
    53.         SelectedSetCompressionBitrate(144000);
    54.     }
    55.  
    56.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/156 (default)")]
    57.     static void SetCompressionBitrate_156kbps() {
    58.         SelectedSetCompressionBitrate(156000);
    59.     }
    60.  
    61.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/160")]
    62.     static void SetCompressionBitrate_160kbps() {
    63.         SelectedSetCompressionBitrate(160000);
    64.     }
    65.  
    66.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/192")]
    67.     static void SetCompressionBitrate_192kbps() {
    68.         SelectedSetCompressionBitrate(192000);
    69.     }
    70.  
    71.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/224")]
    72.     static void SetCompressionBitrate_224kbps() {
    73.         SelectedSetCompressionBitrate(224000);
    74.     }
    75.  
    76.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/240")]
    77.     static void SetCompressionBitrate_240kbps() {
    78.         SelectedSetCompressionBitrate(240000);
    79.     }
    80.  
    81.     // ----------------------------------------------------------------------------
    82.  
    83.     [MenuItem ("Custom/Sound/Toggle decompress on load/Disable")]
    84.     static void ToggleDecompressOnLoad_Disable() {
    85.         SelectedToggleDecompressOnLoadSettings(false);
    86.     }
    87.  
    88.     [MenuItem ("Custom/Sound/Toggle decompress on load/Enable")]
    89.     static void ToggleDecompressOnLoad_Enable() {
    90.         SelectedToggleDecompressOnLoadSettings(true);
    91.     }
    92.  
    93.     // ----------------------------------------------------------------------------
    94.  
    95.     [MenuItem ("Custom/Sound/Toggle 3D sound/Disable")]
    96.     static void Toggle3DSound_Disable() {
    97.         SelectedToggle3DSoundSettings(false);
    98.     }
    99.  
    100.     [MenuItem ("Custom/Sound/Toggle 3D sound/Enable")]
    101.     static void Toggle3DSound_Enable() {
    102.         SelectedToggle3DSoundSettings(true);
    103.     }
    104.  
    105.     // ----------------------------------------------------------------------------
    106.  
    107.     [MenuItem ("Custom/Sound/Toggle mono/Auto")]
    108.     static void ToggleForceToMono_Auto() {
    109.         SelectedToggleForceToMonoSettings(false);
    110.     }
    111.  
    112.     [MenuItem ("Custom/Sound/Toggle mono/Forced")]
    113.     static void ToggleForceToMono_Forced() {
    114.         SelectedToggleForceToMonoSettings(true);
    115.     }
    116.  
    117.     // ----------------------------------------------------------------------------
    118.  
    119.     static void SelectedToggleCompressionSettings(AudioImporterFormat newFormat) {
    120.  
    121.         Object[] audioclips = GetSelectedAudioclips();
    122.         Selection.objects = new Object[0];
    123.         foreach (AudioClip audioclip in audioclips) {
    124.             string path = AssetDatabase.GetAssetPath(audioclip);
    125.             AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
    126.             audioImporter.format = newFormat;
    127.             AssetDatabase.ImportAsset(path);
    128.         }
    129.     }
    130.  
    131.     static void SelectedSetCompressionBitrate(float newCompressionBitrate) {
    132.  
    133.         Object[] audioclips = GetSelectedAudioclips();
    134.         Selection.objects = new Object[0];
    135.         foreach (AudioClip audioclip in audioclips) {
    136.             string path = AssetDatabase.GetAssetPath(audioclip);
    137.             AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
    138.             audioImporter.compressionBitrate = newCompressionBitrate;
    139.             AssetDatabase.ImportAsset(path);
    140.         }
    141.     }
    142.  
    143.     static void SelectedToggleDecompressOnLoadSettings(bool enabled) {
    144.  
    145.         Object[] audioclips = GetSelectedAudioclips();
    146.         Selection.objects = new Object[0];
    147.         foreach (AudioClip audioclip in audioclips) {
    148.             string path = AssetDatabase.GetAssetPath(audioclip);
    149.             AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
    150.             audioImporter.decompressOnLoad = enabled;
    151.             AssetDatabase.ImportAsset(path);
    152.         }
    153.     }
    154.  
    155.     static void SelectedToggle3DSoundSettings(bool enabled) {
    156.  
    157.         Object[] audioclips = GetSelectedAudioclips();
    158.         Selection.objects = new Object[0];
    159.         foreach (AudioClip audioclip in audioclips) {
    160.             string path = AssetDatabase.GetAssetPath(audioclip);
    161.             AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
    162.             audioImporter.threeD = enabled;
    163.             AssetDatabase.ImportAsset(path);
    164.         }
    165.     }
    166.  
    167.     static void SelectedToggleForceToMonoSettings(bool enabled) {
    168.  
    169.         Object[] audioclips = GetSelectedAudioclips();
    170.         Selection.objects = new Object[0];
    171.         foreach (AudioClip audioclip in audioclips) {
    172.             string path = AssetDatabase.GetAssetPath(audioclip);
    173.             AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
    174.             audioImporter.forceToMono = enabled;
    175.             AssetDatabase.ImportAsset(path);
    176.         }
    177.     }
    178.  
    179.     static Object[] GetSelectedAudioclips()
    180.     {
    181.         return Selection.GetFiltered(typeof(AudioClip), SelectionMode.DeepAssets);
    182.     }
    183. }
    184.  
    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.
     
    bugmagnet and Cousken like this.
  2. Ony

    Ony

    Joined:
    Apr 26, 2009
    Posts:
    1,452
    Exactly what I needed right when I needed it. Thanks!
     
  3. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    961
    Same here, awesome! Was just about to create something myself.
     
  4. Evil-Dog

    Evil-Dog

    Joined:
    Oct 4, 2011
    Posts:
    134
    That's awesome thanks a lot.
     
  5. luvcraft

    luvcraft

    Joined:
    Aug 22, 2011
    Posts:
    6
    thanks! I was about to make exactly this same thing but decided to see if anyone had beaten me to it. :)

    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:

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. // /////////////////////////////////////////////////////////////////////////////////////////////////////////
    4. //
    5. // Batch audio import settings modifier.
    6. //
    7. // Modifies all selected audio clips in the project window and applies the requested modification on the
    8. // audio clips. Idea was to have the same choices for multiple files as you would have if you open the
    9. // import settings of a single audio clip. Put this into Assets/Editor and once compiled by Unity you find
    10. // the new functionality in Custom -> Sound. Enjoy! :-)
    11. //
    12. // April 2010. Based on Martin Schultz's texture import settings batch modifier.
    13. //
    14. // /////////////////////////////////////////////////////////////////////////////////////////////////////////
    15. public class ChangeAudioImportSettings : ScriptableObject {
    16.  
    17.     [MenuItem ("Custom/Sound/Toggle audio compression/Disable")]
    18.     static void ToggleCompression_Disable() {
    19.         SelectedToggleCompressionSettings(AudioImporterFormat.Native);
    20.     }
    21.  
    22.     [MenuItem ("Custom/Sound/Toggle audio compression/Enable")]
    23.     static void ToggleCompression_Enable() {
    24.         SelectedToggleCompressionSettings(AudioImporterFormat.Compressed);
    25.     }
    26.  
    27.     // ----------------------------------------------------------------------------
    28.  
    29.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/32")]
    30.     static void SetCompressionBitrate_32kbps() {
    31.         SelectedSetCompressionBitrate(32000);
    32.     }
    33.  
    34.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/64")]
    35.     static void SetCompressionBitrate_64kbps() {
    36.         SelectedSetCompressionBitrate(64000);
    37.     }
    38.  
    39.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/96")]
    40.     static void SetCompressionBitrate_96kbps() {
    41.         SelectedSetCompressionBitrate(96000);
    42.     }
    43.  
    44.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/128")]
    45.     static void SetCompressionBitrate_128kbps() {
    46.         SelectedSetCompressionBitrate(128000);
    47.     }
    48.  
    49.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/144")]
    50.     static void SetCompressionBitrate_144kbps() {
    51.         SelectedSetCompressionBitrate(144000);
    52.     }
    53.  
    54.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/156 (default)")]
    55.     static void SetCompressionBitrate_156kbps() {
    56.         SelectedSetCompressionBitrate(156000);
    57.     }
    58.  
    59.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/160")]
    60.     static void SetCompressionBitrate_160kbps() {
    61.         SelectedSetCompressionBitrate(160000);
    62.     }
    63.  
    64.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/192")]
    65.     static void SetCompressionBitrate_192kbps() {
    66.         SelectedSetCompressionBitrate(192000);
    67.     }
    68.  
    69.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/224")]
    70.     static void SetCompressionBitrate_224kbps() {
    71.         SelectedSetCompressionBitrate(224000);
    72.     }
    73.  
    74.     [MenuItem ("Custom/Sound/Set audio compression bitrate (kbps)/240")]
    75.     static void SetCompressionBitrate_240kbps() {
    76.         SelectedSetCompressionBitrate(240000);
    77.     }
    78.  
    79.     // ----------------------------------------------------------------------------
    80.  
    81.     [MenuItem ("Custom/Sound/Toggle decompress on load/Compressed in memory")]
    82.     static void ToggleDecompressOnLoad_CompressedInMemory() {
    83.         SelectedToggleDecompressOnLoadSettings(AudioImporterLoadType.CompressedInMemory);
    84.     }
    85.  
    86.     [MenuItem("Custom/Sound/Toggle decompress on load/Decompress on load")]
    87.     static void ToggleDecompressOnLoad_DecompressOnLoad() {
    88.         SelectedToggleDecompressOnLoadSettings(AudioImporterLoadType.DecompressOnLoad);
    89.     }
    90.  
    91.     [MenuItem("Custom/Sound/Toggle decompress on load/Stream from disc")]
    92.     static void ToggleDecompressOnLoad_StreamFromDisc() {
    93.         SelectedToggleDecompressOnLoadSettings(AudioImporterLoadType.StreamFromDisc);
    94.     }
    95.  
    96.     // ----------------------------------------------------------------------------
    97.  
    98.     [MenuItem ("Custom/Sound/Toggle 3D sound/Disable")]
    99.     static void Toggle3DSound_Disable() {
    100.         SelectedToggle3DSoundSettings(false);
    101.     }
    102.  
    103.     [MenuItem ("Custom/Sound/Toggle 3D sound/Enable")]
    104.     static void Toggle3DSound_Enable() {
    105.         SelectedToggle3DSoundSettings(true);
    106.     }
    107.  
    108.     // ----------------------------------------------------------------------------
    109.  
    110.     [MenuItem ("Custom/Sound/Toggle mono/Auto")]
    111.     static void ToggleForceToMono_Auto() {
    112.         SelectedToggleForceToMonoSettings(false);
    113.     }
    114.  
    115.     [MenuItem ("Custom/Sound/Toggle mono/Forced")]
    116.     static void ToggleForceToMono_Forced() {
    117.         SelectedToggleForceToMonoSettings(true);
    118.     }
    119.  
    120.     // ----------------------------------------------------------------------------
    121.  
    122.     static void SelectedToggleCompressionSettings(AudioImporterFormat newFormat) {
    123.  
    124.         Object[] audioclips = GetSelectedAudioclips();
    125.         Selection.objects = new Object[0];
    126.         foreach (AudioClip audioclip in audioclips) {
    127.             string path = AssetDatabase.GetAssetPath(audioclip);
    128.             AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
    129.             audioImporter.format = newFormat;
    130.             AssetDatabase.ImportAsset(path);
    131.         }
    132.     }
    133.  
    134.     static void SelectedSetCompressionBitrate(int newCompressionBitrate) {
    135.  
    136.         Object[] audioclips = GetSelectedAudioclips();
    137.         Selection.objects = new Object[0];
    138.         foreach (AudioClip audioclip in audioclips) {
    139.             string path = AssetDatabase.GetAssetPath(audioclip);
    140.             AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
    141.             audioImporter.compressionBitrate = newCompressionBitrate;
    142.             AssetDatabase.ImportAsset(path);
    143.         }
    144.     }
    145.  
    146.     static void SelectedToggleDecompressOnLoadSettings(AudioImporterLoadType loadType) {
    147.  
    148.         Object[] audioclips = GetSelectedAudioclips();
    149.         Selection.objects = new Object[0];
    150.         foreach (AudioClip audioclip in audioclips) {
    151.             string path = AssetDatabase.GetAssetPath(audioclip);
    152.             AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
    153.             audioImporter.loadType = loadType;
    154.             AssetDatabase.ImportAsset(path);
    155.         }
    156.     }
    157.  
    158.     static void SelectedToggle3DSoundSettings(bool enabled) {
    159.  
    160.         Object[] audioclips = GetSelectedAudioclips();
    161.         Selection.objects = new Object[0];
    162.         foreach (AudioClip audioclip in audioclips) {
    163.             string path = AssetDatabase.GetAssetPath(audioclip);
    164.             AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
    165.             audioImporter.threeD = enabled;
    166.             AssetDatabase.ImportAsset(path);
    167.         }
    168.     }
    169.  
    170.     static void SelectedToggleForceToMonoSettings(bool enabled) {
    171.  
    172.         Object[] audioclips = GetSelectedAudioclips();
    173.         Selection.objects = new Object[0];
    174.         foreach (AudioClip audioclip in audioclips) {
    175.             string path = AssetDatabase.GetAssetPath(audioclip);
    176.             AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
    177.             audioImporter.forceToMono = enabled;
    178.             AssetDatabase.ImportAsset(path);
    179.         }
    180.     }
    181.  
    182.     static Object[] GetSelectedAudioclips()
    183.     {
    184.         return Selection.GetFiltered(typeof(AudioClip), SelectionMode.DeepAssets);
    185.     }
    186. }
     
    bugmagnet and Cousken like this.
  6. TwisterK

    TwisterK

    Joined:
    Oct 26, 2010
    Posts:
    110
    Thanks for the awesome script, just what I need.
     
  7. scone

    scone

    Joined:
    May 21, 2008
    Posts:
    244
    I think this is a silly way to do it. Here's mine:

    Code (csharp):
    1.  
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. public class SetAudioSettings : EditorWindow {
    6.  
    7.     private AudioImporterFormat format;
    8.     bool threeD, mono, hardware, gapless;
    9.     private AudioImporterLoadType loadType;
    10.     private int compression = 196;
    11.  
    12.     [MenuItem("Tools/Bulk Audio Settings")]
    13.     private static void Init() {
    14.         GetWindow(typeof(SetAudioSettings));
    15.     }
    16.     void OnGUI() {
    17.         format = (AudioImporterFormat)EditorGUILayout.EnumPopup("Audio Format", format);
    18.         threeD = EditorGUILayout.Toggle("3D sound", threeD);
    19.         mono = EditorGUILayout.Toggle("Force to mono", mono);
    20.         loadType = (AudioImporterLoadType)EditorGUILayout.EnumPopup("Load Type", loadType);
    21.         hardware = EditorGUILayout.Toggle("Hardware decoding", hardware);
    22.         gapless = EditorGUILayout.Toggle("Gapless Looping", gapless);
    23.         compression = EditorGUILayout.IntSlider("Compression (kbps)", compression, 0, 256);
    24.  
    25.         if (GUILayout.Button("Set Settings")) {
    26.             foreach (Object o in Selection.GetFiltered(typeof(AudioClip), SelectionMode.DeepAssets)) {
    27.                 string path = AssetDatabase.GetAssetPath(o);
    28.                 AudioImporter audioImporter = AssetImporter.GetAtPath(path) as AudioImporter;
    29.                 audioImporter.format = format;
    30.                 audioImporter.threeD = threeD;
    31.                 audioImporter.forceToMono = mono;
    32.                 audioImporter.loadType = loadType;
    33.                 audioImporter.hardware = hardware;
    34.                 audioImporter.compressionBitrate = compression * 1000;
    35.                 AssetDatabase.ImportAsset(path);
    36.             }
    37.         }
    38.     }
    39. }
    40.  
     
    larku and Cousken like this.
  8. Cousken

    Cousken

    Joined:
    Jan 19, 2013
    Posts:
    1
    Thanks guys this saved me a couple of hours :)
     
  9. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    271
    Note,

    Around 2015 Unity finally added multiple selection of `AudioClip` -

    so fortunately this problem no longer exists.