Change multiple import settings on multiple files

Discussion in 'Editor' started by bernieR, Aug 11, 2008.

  1. bernieR

    bernieR

    New Member

    Joined:
    Jun 24, 2008
    Messages:
    8
    Is it possible to change the import settings of multiple files of the same type (in this case: wav) at the same time?

    thanks,
    Bernie
     
  2. jashan

    jashan

    Member

    Joined:
    Mar 9, 2007
    Messages:
    2,520
    Hi Bernie,

    I was just looking for the same thing. I had remembered reading something about this in the 2.1 release notes, but I'm afraid it's not as easy as I'd like it to be ...

    From http://unity3d.com/unity/whats-new/unity-2.1 :

    I would think that this would solve it - but on folders, "Assets->Import Settings" is disabled. And obviously, this would only work if there was only one type of assets in the folder; but it seems this is an error, or it doesn't work for sound (that's what I'm trying this with), or ... I just don't get it ;-)

    The documentation on importing assets also doesn't tell me anything more on this.

    However, if you're into programming, there probably is a solution, also from the release notes:

    With that, it should be possible to write a little Editor Script that does this for you...

    Sunny regards,
    Jashan
     
  3. jashan

    jashan

    Member

    Joined:
    Mar 9, 2007
    Messages:
    2,520
    Well, I decided that it is almost quicker and definitely more fun coding up a quick hack that does this for me, so here you go ... but ...

    DISCLAIMER: This is a really quick hack - almost completely untested, so use at your own risk and only use it if you can read the code and understand what it's doing (so you could fix problems that might occur)!!! For more info, see the editor scripting api...

    ... that said: To use it, you need to put it into a folder "Editor" in your project, it's a C# script that must have the name ChangeAudioImportSettings. Once it's compiled you should have a new menu entry "Experimental", with an entry "Change Audio Import Settings" below.

    It's probably best to first try this in some testing environment ;-)

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. /// <summary>
    6. ///     This is a quick hack, but it serves its purpose:
    7. ///     Select the audio files you wish to set the import settings for in
    8. ///     the project view. Open the window, enter your settings, and
    9. ///     click "apply". You can also select an AudioClip and get the settings
    10. ///     of that AudioClip...
    11. /// </summary>
    12. public class ChangeAudioImportSettings : EditorWindow {
    13.     [MenuItem("Experimental/Change Audio Import Settings")]
    14.     public static void InspectSelectionMenuSelected() {
    15.         ChangeAudioImportSettings win = new ChangeAudioImportSettings();
    16.         win.position = new Rect(10, 10, 400, 500);
    17.         win.Show(true);
    18.     }
    19.  
    20.     private Rect guiRect = new Rect(0, 0, 400, 500);
    21.  
    22.     /*
    23.      * I'd rather use System.Enum.GetNames(typeof(AudioImporterFormat)))
    24.      * but that seems to mess things up a bit (probably due to the order
    25.      * of things). ... on the other hand, using "custom text" also has
    26.      * something to it ;-)
    27.      */
    28.     private string[] formatNames = new string[] {
    29.         AudioImporterFormat.Uncompressed.ToString(),
    30.         "Ogg vorbis"
    31.     };
    32.  
    33.     private AudioImporterFormat[] formats = new AudioImporterFormat[] {
    34.         AudioImporterFormat.Uncompressed,
    35.         AudioImporterFormat.OggVorbis
    36.     };
    37.  
    38.     private string[] channelTypeNames = new string[] {
    39.         "As supplied by File",
    40.         "Force Mono",
    41.         "Force Stereo"
    42.     };
    43.  
    44.     private AudioImporterChannels[] channelTypes = new AudioImporterChannels[] {
    45.         AudioImporterChannels.Automatic,
    46.         AudioImporterChannels.Mono,
    47.         AudioImporterChannels.Stereo
    48.     };
    49.  
    50.     private AudioImporterFormat format = AudioImporterFormat.Automatic;
    51.     private AudioImporterChannels channels = AudioImporterChannels.Automatic;
    52.     private float compressionBitRate = 128000F;
    53.     private bool decompressOnLoad = false;
    54.  
    55.     public ChangeAudioImportSettings() { }
    56.  
    57.     public void OnSelectionChange() {
    58.         this.Repaint();
    59.     }
    60.  
    61.     public void OnGUI() {
    62.         GUI.skin = EditorGUIUtility.GetBuiltinSkin(EditorSkin.Inspector);
    63.  
    64.         GUILayout.BeginArea(guiRect);
    65.         GUILayout.BeginVertical();
    66.  
    67.         DrawCurrentValues();
    68.  
    69.         GUILayout.Space(10F);
    70.  
    71.         GUILayout.BeginHorizontal();
    72.         if (GUILayout.Button("Close")) {
    73.             this.Close();
    74.         }
    75.         if (GUILayout.Button("Get Values!")) {
    76.             HandleButtonClickGetValues();
    77.         }
    78.         if (GUILayout.Button("Apply!")) {
    79.             HandleButtonClickApply();
    80.         }
    81.         GUILayout.EndHorizontal();
    82.  
    83.         GUILayout.Space(15F);
    84.  
    85.         // this is in here so that you (hopefully) know what you're doing ;-)
    86.         DrawCurrentSelection();
    87.  
    88.         GUILayout.EndVertical();
    89.         GUILayout.FlexibleSpace();
    90.         GUILayout.EndArea();
    91.     }
    92.  
    93.     private void HandleButtonClickGetValues() {
    94.         if (Selection.activeObject != null  Selection.activeObject is AudioClip) {
    95.             string path = AssetDatabase.GetAssetPath(Selection.activeObject);
    96.             AudioImporter audioImporter = (AudioImporter)AssetImporter.GetAtPath(path);
    97.             this.format = audioImporter.format;
    98.             this.channels = audioImporter.channels;
    99.             this.compressionBitRate = audioImporter.compressionBitrate;
    100.             this.decompressOnLoad = audioImporter.decompressOnLoad;
    101.             this.Repaint();
    102.         }
    103.     }
    104.  
    105.     private void HandleButtonClickApply() {
    106.         if (Selection.objects != null) {
    107.             foreach (Object obj in Selection.objects) {
    108.                 if (obj is AudioClip) {
    109.                     string path = AssetDatabase.GetAssetPath(obj);
    110.                     AudioImporter audioImporter
    111.                         = (AudioImporter)AssetImporter.GetAtPath(path);
    112.                     audioImporter.format = this.format;
    113.                     audioImporter.channels = this.channels;
    114.                     audioImporter.compressionBitrate = this.compressionBitRate;
    115.                     audioImporter.decompressOnLoad = this.decompressOnLoad;
    116.                 }
    117.             }
    118.         }
    119.     }
    120.  
    121.     private void DrawCurrentValues() {
    122.         int formatIndex = 0;
    123.         // there MUST be a better way to do this :-/
    124.         // (and I'm not talking about using a dictionary ;-) )
    125.         for (int i = 0; i < formats.Length; i++) {
    126.             if (formats[i] == this.format) {
    127.                 formatIndex = i;
    128.             }
    129.         }
    130.            
    131.         formatIndex = EditorGUILayout.Popup("Format:", formatIndex, formatNames);
    132.        
    133.         this.format = formats[formatIndex];
    134.  
    135.        
    136.         int channelIndex = 0;
    137.         for (int i = 0; i < channelTypes.Length; i++) {
    138.             if (channelTypes[i] == this.channels) {
    139.                 channelIndex = i;
    140.             }
    141.         }
    142.  
    143.         channelIndex = EditorGUILayout.Popup("Channels:", channelIndex, channelTypeNames);
    144.  
    145.         this.channels = channelTypes[channelIndex];
    146.  
    147.  
    148.         compressionBitRate = (float)(1000 * (int)EditorGUILayout.Slider(
    149.             "CompressionBitrate:",
    150.             this.compressionBitRate * 0.001F, 15F, 315F));
    151.  
    152.         decompressOnLoad = EditorGUILayout.Toggle("Decompress on Load?", decompressOnLoad);
    153.     }
    154.  
    155.     private void DrawCurrentSelection() {
    156.         if (Selection.objects != null) {
    157.             GUILayout.Label("Current selection:");
    158.             foreach (Object obj in Selection.objects) {
    159.                 GUILayout.Label(string.Format("{0}: {1}", obj.GetType().Name, obj.name));
    160.             }
    161.         }
    162.     }
    163.  
    164.  
    165. }
    Sunny regards,
    Jashan