ChangeTextureImportSettings.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEngine.UI;
  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. [MenuItem ("Custom/Texture/Change Texture Format/Auto Compressed")]
  25. static void ChangeTextureFormat_AutoCompressed() {
  26. SelectedChangeTextureFormatSettings(TextureImporterFormat.AutomaticCompressed);
  27. }
  28. [MenuItem ("Custom/Texture/Change Texture Format/Auto 16bit")]
  29. static void ChangeTextureFormat_Auto16Bit() {
  30. SelectedChangeTextureFormatSettings(TextureImporterFormat.Automatic16bit);
  31. }
  32. [MenuItem ("Custom/Texture/Change Texture Format/Auto Truecolor")]
  33. static void ChangeTextureFormat_AutoTruecolor() {
  34. SelectedChangeTextureFormatSettings(TextureImporterFormat.AutomaticTruecolor);
  35. }
  36. [MenuItem ("Custom/Texture/Change Texture Format/RGB Compressed DXT1")]
  37. static void ChangeTextureFormat_RGB_DXT1() {
  38. SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT1);
  39. }
  40. [MenuItem ("Custom/Texture/Change Texture Format/RGB Compressed DXT5")]
  41. static void ChangeTextureFormat_RGB_DXT5() {
  42. SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT5);
  43. }
  44. [MenuItem ("Custom/Texture/Change Texture Format/RGB 16 bit")]
  45. static void ChangeTextureFormat_RGB_16bit() {
  46. SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB16);
  47. }
  48. [MenuItem ("Custom/Texture/Change Texture Format/RGB 24 bit")]
  49. static void ChangeTextureFormat_RGB_24bit() {
  50. SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB24);
  51. }
  52. [MenuItem ("Custom/Texture/Change Texture Format/Alpha 8 bit")]
  53. static void ChangeTextureFormat_Alpha_8bit() {
  54. SelectedChangeTextureFormatSettings(TextureImporterFormat.Alpha8);
  55. }
  56. [MenuItem ("Custom/Texture/Change Texture Format/ARGB 16 bit")]
  57. static void ChangeTextureFormat_RGBA_16bit() {
  58. SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB16);
  59. }
  60. [MenuItem ("Custom/Texture/Change Texture Format/RGBA 32 bit")]
  61. static void ChangeTextureFormat_RGBA_32bit() {
  62. SelectedChangeTextureFormatSettings(TextureImporterFormat.RGBA32);
  63. }
  64. [MenuItem ("Custom/Texture/Change Texture Format/ARGB 32 bit")]
  65. static void ChangeTextureFormat_ARGB_32bit() {
  66. SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB32);
  67. }
  68. [MenuItem ("Custom/Texture/Change Texture Format/RGB PVRTC 2bit")]
  69. static void ChangeTextureFormat_RGB_PVRTC_2bit() {
  70. SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGB2);
  71. }
  72. [MenuItem ("Custom/Texture/Change Texture Format/RGBA PVRTC 2bit")]
  73. static void ChangeTextureFormat_RGBA_PVRTC_2bit() {
  74. SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGBA2);
  75. }
  76. [MenuItem ("Custom/Texture/Change Texture Format/RGB PVRTC 4bit")]
  77. static void ChangeTextureFormat_RGB_PVRTC_4bit() {
  78. SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGB4);
  79. }
  80. [MenuItem ("Custom/Texture/Change Texture Format/RGBA PVRTC 4bit")]
  81. static void ChangeTextureFormat_RGBA_PVRTC_4bit() {
  82. SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGBA4);
  83. }
  84. // ----------------------------------------------------------------------------
  85. [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/32")]
  86. static void ChangeTextureSize_32() {
  87. SelectedChangeMaxTextureSize(32);
  88. }
  89. [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/64")]
  90. static void ChangeTextureSize_64() {
  91. SelectedChangeMaxTextureSize(64);
  92. }
  93. [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/128")]
  94. static void ChangeTextureSize_128() {
  95. SelectedChangeMaxTextureSize(128);
  96. }
  97. [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/256")]
  98. static void ChangeTextureSize_256() {
  99. SelectedChangeMaxTextureSize(256);
  100. }
  101. [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/512")]
  102. static void ChangeTextureSize_512() {
  103. SelectedChangeMaxTextureSize(512);
  104. }
  105. [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/1024")]
  106. static void ChangeTextureSize_1024() {
  107. SelectedChangeMaxTextureSize(1024);
  108. }
  109. [MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/2048")]
  110. static void ChangeTextureSize_2048() {
  111. SelectedChangeMaxTextureSize(2048);
  112. }
  113. // ----------------------------------------------------------------------------
  114. [MenuItem ("Custom/Texture/Change MipMap/Enable MipMap")]
  115. static void ChangeMipMap_On() {
  116. SelectedChangeMimMap(true);
  117. }
  118. [MenuItem ("Custom/Texture/Change MipMap/Disable MipMap")]
  119. static void ChangeMipMap_Off() {
  120. SelectedChangeMimMap(false);
  121. }
  122. // ----------------------------------------------------------------------------
  123. [MenuItem ("Custom/Texture/Change Non Power of 2/None")]
  124. static void ChangeNPOT_None() {
  125. SelectedChangeNonPowerOf2(TextureImporterNPOTScale.None);
  126. }
  127. [MenuItem ("Custom/Texture/Change Non Power of 2/ToNearest")]
  128. static void ChangeNPOT_ToNearest() {
  129. SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToNearest);
  130. }
  131. [MenuItem ("Custom/Texture/Change Non Power of 2/ToLarger")]
  132. static void ChangeNPOT_ToLarger() {
  133. SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToLarger);
  134. }
  135. [MenuItem ("Custom/Texture/Change Non Power of 2/ToSmaller")]
  136. static void ChangeNPOT_ToSmaller() {
  137. SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToSmaller);
  138. }
  139. // ----------------------------------------------------------------------------
  140. [MenuItem ("Custom/Texture/Change Is Readable/Enable")]
  141. static void ChangeIsReadable_Yes() {
  142. SelectedChangeIsReadable(true);
  143. }
  144. [MenuItem ("Custom/Texture/Change Is Readable/Disable")]
  145. static void ChangeIsReadable_No() {
  146. SelectedChangeIsReadable(false);
  147. } //Unity3D教程手册:www.unitymanual.com
  148. // ----------------------------------------------------------------------------
  149. static void SelectedChangeIsReadable(bool enabled) {
  150. Object[] textures = GetSelectedTextures();
  151. Selection.objects = new Object[0];
  152. foreach (Texture2D texture in textures) {
  153. string path = AssetDatabase.GetAssetPath(texture);
  154. TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
  155. textureImporter.isReadable = enabled;
  156. AssetDatabase.ImportAsset(path);
  157. }
  158. }
  159. static void SelectedChangeNonPowerOf2(TextureImporterNPOTScale npot) {
  160. Object[] textures = GetSelectedTextures();
  161. Selection.objects = new Object[0];
  162. foreach (Texture2D texture in textures) {
  163. string path = AssetDatabase.GetAssetPath(texture);
  164. TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
  165. textureImporter.npotScale = npot;
  166. AssetDatabase.ImportAsset(path);
  167. }
  168. }
  169. static void SelectedChangeMimMap(bool enabled) {
  170. Object[] textures = GetSelectedTextures();
  171. Selection.objects = new Object[0];
  172. foreach (Texture2D texture in textures) {
  173. string path = AssetDatabase.GetAssetPath(texture);
  174. TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
  175. textureImporter.mipmapEnabled = enabled;
  176. AssetDatabase.ImportAsset(path);
  177. }
  178. }
  179. //Unity3D教程手册:www.unitymanual.com
  180. static void SelectedChangeMaxTextureSize(int size) {
  181. Object[] textures = GetSelectedTextures();
  182. Selection.objects = new Object[0];
  183. foreach (Texture2D texture in textures) {
  184. string path = AssetDatabase.GetAssetPath(texture);
  185. TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
  186. textureImporter.maxTextureSize = size;
  187. AssetDatabase.ImportAsset(path);
  188. }
  189. }
  190. static void SelectedChangeTextureFormatSettings(TextureImporterFormat newFormat) {
  191. Object[] textures = GetSelectedTextures();
  192. Selection.objects = new Object[0];
  193. foreach (Texture2D texture in textures) {
  194. string path = AssetDatabase.GetAssetPath(texture);
  195. //Debug.Log("path: " + path);
  196. TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
  197. textureImporter.textureFormat = newFormat;
  198. AssetDatabase.ImportAsset(path);
  199. }
  200. }
  201. static Object[] GetSelectedTextures()
  202. {
  203. return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
  204. }
  205. }