Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 3rd, 2012  |  syntax: None  |  size: 10.11 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. // /////////////////////////////////////////////////////////////////////////////////////////////////////////
  5. //
  6. // Batch Texture import settings modifier.
  7. //
  8. // Modifies all selected textures in the project window and applies the requested modification on the
  9. // textures. Idea was to have the same choices for multiple files as you would have if you open the
  10. // import settings of a single texture. Put this into Assets/Editor and once compiled by Unity you find
  11. // the new functionality in Custom -> Texture. Enjoy! :-)
  12. //
  13. // Based on the great work of benblo in this thread:
  14. // http://forum.unity3d.com/viewtopic.php?t=16079&start=0&postdays=0&postorder=asc&highlight=textureimporter
  15. //
  16. // Developed by Martin Schultz, Decane in August 2009
  17. // e-mail: ms@decane.net
  18. //
  19. // Updated for Unity 3.0 by col000r in August 2010
  20. // http://col000r.blogspot.com
  21. //
  22. // /////////////////////////////////////////////////////////////////////////////////////////////////////////
  23. public class ChangeTextureImportSettingsUnity3 : ScriptableObject
  24. {
  25.  
  26.     [MenuItem( "Custom/Texture/Change Texture Format/Auto Compressed" )]
  27.     static void ChangeTextureFormat_AutoCompressed()
  28.     {
  29.         SelectedChangeTextureFormatSettings( TextureImporterFormat.AutomaticCompressed );
  30.     }
  31.  
  32.     [MenuItem( "Custom/Texture/Change Texture Format/Auto 16bit" )]
  33.     static void ChangeTextureFormat_Auto16Bit()
  34.     {
  35.         SelectedChangeTextureFormatSettings( TextureImporterFormat.Automatic16bit );
  36.     }
  37.  
  38.     [MenuItem( "Custom/Texture/Change Texture Format/Auto Truecolor" )]
  39.     static void ChangeTextureFormat_AutoTruecolor()
  40.     {
  41.         SelectedChangeTextureFormatSettings( TextureImporterFormat.AutomaticTruecolor );
  42.     }
  43.  
  44.     [MenuItem( "Custom/Texture/Change Texture Format/RGB Compressed DXT1" )]
  45.     static void ChangeTextureFormat_RGB_DXT1()
  46.     {
  47.         SelectedChangeTextureFormatSettings( TextureImporterFormat.DXT1 );
  48.     }
  49.  
  50.     [MenuItem( "Custom/Texture/Change Texture Format/RGB Compressed DXT5" )]
  51.     static void ChangeTextureFormat_RGB_DXT5()
  52.     {
  53.         SelectedChangeTextureFormatSettings( TextureImporterFormat.DXT5 );
  54.     }
  55.  
  56.     [MenuItem( "Custom/Texture/Change Texture Format/RGB 16 bit" )]
  57.     static void ChangeTextureFormat_RGB_16bit()
  58.     {
  59.         SelectedChangeTextureFormatSettings( TextureImporterFormat.RGB16 );
  60.     }
  61.  
  62.     [MenuItem( "Custom/Texture/Change Texture Format/RGB 24 bit" )]
  63.     static void ChangeTextureFormat_RGB_24bit()
  64.     {
  65.         SelectedChangeTextureFormatSettings( TextureImporterFormat.RGB24 );
  66.     }
  67.  
  68.     [MenuItem( "Custom/Texture/Change Texture Format/Alpha 8 bit" )]
  69.     static void ChangeTextureFormat_Alpha_8bit()
  70.     {
  71.         SelectedChangeTextureFormatSettings( TextureImporterFormat.Alpha8 );
  72.     }
  73.  
  74.     [MenuItem( "Custom/Texture/Change Texture Format/ARGB 16 bit" )]
  75.     static void ChangeTextureFormat_RGBA_16bit()
  76.     {
  77.         SelectedChangeTextureFormatSettings( TextureImporterFormat.ARGB16 );
  78.     }
  79.  
  80.     [MenuItem( "Custom/Texture/Change Texture Format/RGBA 32 bit" )]
  81.     static void ChangeTextureFormat_RGBA_32bit()
  82.     {
  83.         SelectedChangeTextureFormatSettings( TextureImporterFormat.RGBA32 );
  84.     }
  85.  
  86.     [MenuItem( "Custom/Texture/Change Texture Format/ARGB 32 bit" )]
  87.     static void ChangeTextureFormat_ARGB_32bit()
  88.     {
  89.         SelectedChangeTextureFormatSettings( TextureImporterFormat.ARGB32 );
  90.     }
  91.  
  92.     [MenuItem( "Custom/Texture/Change Texture Format/RGB PVRTC 2bit" )]
  93.     static void ChangeTextureFormat_RGB_PVRTC_2bit()
  94.     {
  95.         SelectedChangeTextureFormatSettings( TextureImporterFormat.PVRTC_RGB2 );
  96.     }
  97.  
  98.     [MenuItem( "Custom/Texture/Change Texture Format/RGBA PVRTC 2bit" )]
  99.     static void ChangeTextureFormat_RGBA_PVRTC_2bit()
  100.     {
  101.         SelectedChangeTextureFormatSettings( TextureImporterFormat.PVRTC_RGBA2 );
  102.     }
  103.  
  104.     [MenuItem( "Custom/Texture/Change Texture Format/RGB PVRTC 4bit" )]
  105.     static void ChangeTextureFormat_RGB_PVRTC_4bit()
  106.     {
  107.         SelectedChangeTextureFormatSettings( TextureImporterFormat.PVRTC_RGB4 );
  108.     }
  109.  
  110.     [MenuItem( "Custom/Texture/Change Texture Format/RGBA PVRTC 4bit" )]
  111.     static void ChangeTextureFormat_RGBA_PVRTC_4bit()
  112.     {
  113.         SelectedChangeTextureFormatSettings( TextureImporterFormat.PVRTC_RGBA4 );
  114.     }
  115.  
  116.     // ----------------------------------------------------------------------------
  117.  
  118.     [MenuItem( "Custom/Texture/Change Texture Size/Change Max Texture Size/32" )]
  119.     static void ChangeTextureSize_32()
  120.     {
  121.         SelectedChangeMaxTextureSize( 32 );
  122.     }
  123.  
  124.     [MenuItem( "Custom/Texture/Change Texture Size/Change Max Texture Size/64" )]
  125.     static void ChangeTextureSize_64()
  126.     {
  127.         SelectedChangeMaxTextureSize( 64 );
  128.     }
  129.  
  130.     [MenuItem( "Custom/Texture/Change Texture Size/Change Max Texture Size/128" )]
  131.     static void ChangeTextureSize_128()
  132.     {
  133.         SelectedChangeMaxTextureSize( 128 );
  134.     }
  135.  
  136.     [MenuItem( "Custom/Texture/Change Texture Size/Change Max Texture Size/256" )]
  137.     static void ChangeTextureSize_256()
  138.     {
  139.         SelectedChangeMaxTextureSize( 256 );
  140.     }
  141.  
  142.     [MenuItem( "Custom/Texture/Change Texture Size/Change Max Texture Size/512" )]
  143.     static void ChangeTextureSize_512()
  144.     {
  145.         SelectedChangeMaxTextureSize( 512 );
  146.     }
  147.  
  148.     [MenuItem( "Custom/Texture/Change Texture Size/Change Max Texture Size/1024" )]
  149.     static void ChangeTextureSize_1024()
  150.     {
  151.         SelectedChangeMaxTextureSize( 1024 );
  152.     }
  153.  
  154.     [MenuItem( "Custom/Texture/Change Texture Size/Change Max Texture Size/2048" )]
  155.     static void ChangeTextureSize_2048()
  156.     {
  157.         SelectedChangeMaxTextureSize( 2048 );
  158.     }
  159.  
  160.     // ----------------------------------------------------------------------------
  161.  
  162.     [MenuItem( "Custom/Texture/Change MipMap/Enable MipMap" )]
  163.     static void ChangeMipMap_On()
  164.     {
  165.         SelectedChangeMimMap( true );
  166.     }
  167.  
  168.     [MenuItem( "Custom/Texture/Change MipMap/Disable MipMap" )]
  169.     static void ChangeMipMap_Off()
  170.     {
  171.         SelectedChangeMimMap( false );
  172.     }
  173.  
  174.     // ----------------------------------------------------------------------------
  175.  
  176.  
  177.     [MenuItem( "Custom/Texture/Change Non Power of 2/None" )]
  178.     static void ChangeNPOT_None()
  179.     {
  180.         SelectedChangeNonPowerOf2( TextureImporterNPOTScale.None );
  181.     }
  182.  
  183.     [MenuItem( "Custom/Texture/Change Non Power of 2/ToNearest" )]
  184.     static void ChangeNPOT_ToNearest()
  185.     {
  186.         SelectedChangeNonPowerOf2( TextureImporterNPOTScale.ToNearest );
  187.     }
  188.  
  189.     [MenuItem( "Custom/Texture/Change Non Power of 2/ToLarger" )]
  190.     static void ChangeNPOT_ToLarger()
  191.     {
  192.         SelectedChangeNonPowerOf2( TextureImporterNPOTScale.ToLarger );
  193.     }
  194.  
  195.     [MenuItem( "Custom/Texture/Change Non Power of 2/ToSmaller" )]
  196.     static void ChangeNPOT_ToSmaller()
  197.     {
  198.         SelectedChangeNonPowerOf2( TextureImporterNPOTScale.ToSmaller );
  199.     }
  200.  
  201.     // ----------------------------------------------------------------------------
  202.  
  203.     [MenuItem( "Custom/Texture/Change Is Readable/Enable" )]
  204.     static void ChangeIsReadable_Yes()
  205.     {
  206.         SelectedChangeIsReadable( true );
  207.     }
  208.  
  209.     [MenuItem( "Custom/Texture/Change Is Readable/Disable" )]
  210.     static void ChangeIsReadable_No()
  211.     {
  212.         SelectedChangeIsReadable( false );
  213.     }
  214.  
  215.     // ----------------------------------------------------------------------------
  216.  
  217.  
  218.  
  219.  
  220.     static void SelectedChangeIsReadable( bool enabled )
  221.     {
  222.  
  223.         Object[] textures = GetSelectedTextures();
  224.         Selection.objects = new Object[0];
  225.         foreach( Texture2D texture in textures )
  226.         {
  227.             string path = AssetDatabase.GetAssetPath( texture );
  228.             TextureImporter textureImporter = AssetImporter.GetAtPath( path ) as TextureImporter;
  229.             textureImporter.isReadable = enabled;
  230.             AssetDatabase.ImportAsset( path );
  231.         }
  232.     }
  233.  
  234.  
  235.     static void SelectedChangeNonPowerOf2( TextureImporterNPOTScale npot )
  236.     {
  237.  
  238.         Object[] textures = GetSelectedTextures();
  239.         Selection.objects = new Object[0];
  240.         foreach( Texture2D texture in textures )
  241.         {
  242.             string path = AssetDatabase.GetAssetPath( texture );
  243.             TextureImporter textureImporter = AssetImporter.GetAtPath( path ) as TextureImporter;
  244.             textureImporter.npotScale = npot;
  245.             AssetDatabase.ImportAsset( path );
  246.         }
  247.     }
  248.  
  249.     static void SelectedChangeMimMap( bool enabled )
  250.     {
  251.  
  252.         Object[] textures = GetSelectedTextures();
  253.         Selection.objects = new Object[0];
  254.         foreach( Texture2D texture in textures )
  255.         {
  256.             string path = AssetDatabase.GetAssetPath( texture );
  257.             TextureImporter textureImporter = AssetImporter.GetAtPath( path ) as TextureImporter;
  258.             textureImporter.mipmapEnabled = enabled;
  259.             AssetDatabase.ImportAsset( path );
  260.         }
  261.     }
  262.  
  263.     static void SelectedChangeMaxTextureSize( int size )
  264.     {
  265.  
  266.         Object[] textures = GetSelectedTextures();
  267.         Selection.objects = new Object[0];
  268.         foreach( Texture2D texture in textures )
  269.         {
  270.             string path = AssetDatabase.GetAssetPath( texture );
  271.             TextureImporter textureImporter = AssetImporter.GetAtPath( path ) as TextureImporter;
  272.             textureImporter.maxTextureSize = size;
  273.             AssetDatabase.ImportAsset( path );
  274.         }
  275.     }
  276.  
  277.     static void SelectedChangeTextureFormatSettings( TextureImporterFormat newFormat )
  278.     {
  279.  
  280.         Object[] textures = GetSelectedTextures();
  281.         Selection.objects = new Object[0];
  282.         foreach( Texture2D texture in textures )
  283.         {
  284.             string path = AssetDatabase.GetAssetPath( texture );
  285.             //Debug.Log("path: " + path);
  286.             TextureImporter textureImporter = AssetImporter.GetAtPath( path ) as TextureImporter;
  287.             textureImporter.textureFormat = newFormat;
  288.             AssetDatabase.ImportAsset( path );
  289.         }
  290.     }
  291.  
  292.     static Object[] GetSelectedTextures()
  293.     {
  294.         return Selection.GetFiltered( typeof( Texture2D ), SelectionMode.DeepAssets );
  295.     }
  296. }