Search Unity

Mass texture importer

Discussion in 'iOS and tvOS' started by benblo, Nov 19, 2008.

  1. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    A while ago I made an editor script to batch-import a set of textures at once (I used it to automatically remove mipmaps and clamp GUI textures).

    I figured it would work well to automatically set texture format to PVRTC for iPhone compatibility... except TextureImporterFormat doesn't expose iPhone-specific formats (logged as bug 51814).

    Never mind, leaving the format to "Automatic" reimports textures as PVRTC 4-bit RGBA by default so it's a start. I guess it could be useful to other people so here it is...
    Just importe the attached package, or copy-paste the follwing script in an Editor folder, and an Editor/Texture menu will appear. Select the textures you want to reimport (individual ones or whole folders), and run the wizard or just the quick options... voilà!

    Code (csharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. class MassImportTexture : ScriptableWizard
    5. {
    6.     public bool changeFormat;
    7.     public TextureImporterFormat textureFormat = TextureImporterFormat.Automatic;
    8.  
    9.     public bool changeMipmaps;
    10.     public bool mipmapsEnabled;
    11.  
    12.     public bool changeWrapMode;
    13.     public TextureWrapMode wrapMode;
    14.  
    15.     void OnWizardUpdate()
    16.     {
    17.         // are the properties valid?
    18.         bool propertiesValid = changeFormat || changeMipmaps || changeWrapMode;
    19.  
    20.         // is the selection valid?
    21.         Object[] textures = GetSelectedTextures();
    22.         bool selectionValid = textures.Length > 0;
    23.  
    24.  
    25.         // help
    26.         isValid = propertiesValid  selectionValid;
    27.  
    28.         errorString = !propertiesValid ? "Select at least one thing to change..." :
    29.             !selectionValid ? "Select at least one texture" : "";
    30.  
    31.         helpString = textures.Length + " texture(s) will be reimported.";
    32.     }
    33.  
    34.     void OnWizardCreate()
    35.     {
    36.         // code when first button is clicked
    37.         apply();
    38.     }
    39.     void OnWizardOtherButton()
    40.     {
    41.         // code when second button is clicked
    42.         apply();
    43.     }
    44.     void apply()
    45.     {
    46.  
    47.         ProcessSelection(
    48.             changeFormat, textureFormat,
    49.             changeMipmaps, mipmapsEnabled,
    50.             changeWrapMode, wrapMode);
    51.     }
    52.  
    53.  
    54.     [MenuItem("Editor/Texture/Mass import...")]
    55.     static void CreateWizard()
    56.     {
    57.         ScriptableWizard.DisplayWizard(
    58.             "Mass import textures", typeof(MassImportTexture),
    59.             "Apply  Close", "Apply"
    60.         );
    61.     }
    62.  
    63.     [MenuItem("Editor/Texture/Remove mipmaps + clamp")]
    64.     static void RemoveMipmapsAndClamp()
    65.     {
    66.         ProcessSelection(
    67.             false, TextureImporterFormat.Automatic,
    68.             true, false,
    69.             true, TextureWrapMode.Clamp);
    70.     }
    71.  
    72.     [MenuItem("Editor/Texture/Remove mipmaps")]
    73.     static void RemoveMipmaps()
    74.     {
    75.         ProcessSelection(
    76.             false, TextureImporterFormat.Automatic,
    77.             true, false,
    78.             false, 0);
    79.     }
    80.  
    81.     [MenuItem("Editor/Texture/Clamp")]
    82.     static void Clamp()
    83.     {
    84.         ProcessSelection(
    85.             false, TextureImporterFormat.Automatic,
    86.             false, false,
    87.             true, TextureWrapMode.Clamp);
    88.     }
    89.  
    90.     [MenuItem("Editor/Texture/Convert to iPhone (PVRTC 4-bits RGBA)")]
    91.     static void ConvertToIPhone()
    92.     {
    93.         ProcessSelection(
    94.             true, TextureImporterFormat.Automatic,
    95.             false, false,
    96.             false, 0);
    97.     }
    98.  
    99.     static Object[] GetSelectedTextures()
    100.     {
    101.         return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
    102.     }
    103.     static void ProcessSelection(bool changeFormat, TextureImporterFormat textureFormat, bool changeMipmaps, bool mipmapsEnabled, bool changeWrapMode, TextureWrapMode wrapMode)
    104.     {
    105.         if (Selection.objects.Length > 0)
    106.         {
    107.             Object[] textures = GetSelectedTextures();
    108.  
    109.             if (textures.Length > 0)
    110.             {
    111.                 foreach (Texture2D texture in textures)
    112.                 {
    113.                     if (changeFormat || changeMipmaps)
    114.                     {
    115.                         string path = AssetDatabase.GetAssetPath(texture);
    116.                         //Debug.Log("RemoveMipmaps : " + path);
    117.  
    118.                         TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
    119.  
    120.                         if (changeFormat)
    121.                             textureImporter.textureFormat = textureFormat;
    122.  
    123.                         if (changeMipmaps)
    124.                             textureImporter.mipmapEnabled = mipmapsEnabled;
    125.  
    126.                         AssetDatabase.ImportAsset(path);
    127.                     }
    128.  
    129.                     if (changeWrapMode)
    130.                     {
    131.                         texture.wrapMode = wrapMode;
    132.                     }
    133.                 }
    134.  
    135.                 Debug.Log(string.Format(
    136.                     "{0} texture(s) reimported.{1}{2}{3}",
    137.                     textures.Length,
    138.                     changeFormat ? "\nformat set to " + textureFormat : "",
    139.                     changeMipmaps ? "\nmipmaps turned " + (mipmapsEnabled ? "ON" : "OFF") : "",
    140.                     changeWrapMode ? "\nwrap mode set to " + wrapMode : ""
    141.                 ));
    142.             }
    143.             else
    144.             {
    145.                 EditorUtility.DisplayDialog("No textures selected!", "No textures found in selected objects.", "OK");
    146.             }
    147.         }
    148.         else
    149.         {
    150.             EditorUtility.DisplayDialog("No object selected!", "Select at least one object.", "OK");
    151.         }
    152.     }
    153. }
     

    Attached Files:

  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    thank you for sharing this script
     
  3. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    you're welcome :) !
     
  4. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Wonderful!
     
  5. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Awsum! Put it on the wiki? :D
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Cool...I was going to write something like this but now I guess I don't have to. :)

    --Eric
     
  7. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    When I got time™... aka Gods know when :D
    No but seriously, I'm glad you guys find it useful, I'll probably extend it a little to add more options, then throw it on the wiki.
     
  8. pinchmasterflex

    pinchmasterflex

    Joined:
    Jan 25, 2009
    Posts:
    5
    fantasic work just wondering has some one created a script like this to edit multiple texture in Unity (non iPhone)
     
  9. box01

    box01

    Joined:
    Apr 2, 2009
    Posts:
    62
    can some one add a addition function to change the max texture size as well?
     
  10. benblo

    benblo

    Joined:
    Aug 14, 2007
    Posts:
    476
    Here's my latest version*... adds an Editor/Texture submenu with multiple default options, and a wizard.
    Doesn't play nice if a texture is selected in the inspector, better to work on a folder.

    TODO:
    • make it a window instead of a wizard (cooler formatting)
    • maybe move/copy the menu to Assets menu so it's accessible on right-click in project view?
    • make a postprocessor version for automating per-folder settings.

    * I have multiple versions of this script lying around , every time I have a new need I add it in the project it's in, and I always fail to merge the changes... has anyone come up with a silver bullet for synching?
     

    Attached Files:

  11. cupsster

    cupsster

    Joined:
    Apr 14, 2009
    Posts:
    363
    quite usefull package.. thank you for sharing..
    learning all the way.. ;)
     
  12. rahuxx

    rahuxx

    Joined:
    May 8, 2009
    Posts:
    537
    can you make something similar for mass mesh/object importer and then one can manage them all as a sequence?
     
  13. kedi189

    kedi189

    Joined:
    Oct 15, 2009
    Posts:
    7
    thank you so much for sharing. this script has saved us a lot of time.
     
  14. Elowan

    Elowan

    Joined:
    Aug 14, 2013
    Posts:
    106
    Using Unity 3.4.0 (free) here.... imported the last package, but nothing shows up in Menu or elsewhere....
    Compiler reports
    Code (csharp):
    1. Assets/Editor/MassTextureImporter.cs(11,76): error CS0117: `UnityEditor.TextureImporterFormat' does not contain a definition for `Automatic'
    2.  
    Ok, there is an update on the wiki for Unity 3.0 (this works with 3.4 also!)
     
    Last edited: Dec 6, 2013