纳金网

标题: Unity3D 批量图片资源导入设置 [打印本页]

作者: 王者再临    时间: 2015-2-27 00:51
标题: Unity3D 批量图片资源导入设置

Unity3D 批量图片资源导入设置
  1. using UnityEngine;

  2. using System.Collections;

  3. using UnityEditor;

  4. /// <summary>

  5. /// 批量图片资源导入设置

  6. /// 使用说明: 选择需要批量设置的贴图,

  7. /// 单击DuanMenu/Texture Import Settings,

  8. /// 打开窗口后选择对应参数,

  9. /// 点击Set Texture ImportSettings,

  10. /// 稍等片刻,--批量设置成功。

  11. /// </summary>

  12. public class TextureImportSetting : EditorWindow {

  13. /// <summary>

  14. /// 临时存储int[]

  15. /// </summary>

  16. private int[] IntArray = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };

  17. //AnisoLevel

  18. private int AnisoLevel = 1;

  19. //Filter Mode

  20. private int FilterModeInt = 0;

  21. private string[] FilterModeString = new string[] { “Point”, “Bilinear”, “Trilinear” };

  22. //Wrap Mode

  23. private int WrapModeInt = 0;

  24. private string[] WrapModeString = new string[] { “Repeat”, “Clamp” };

  25. //Texture Type

  26. private int TextureTypeInt = 0;

  27. private string[] TextureTypeString = new string[] { “Texture”, “Normal Map”, “GUI”, “Refelection”, “Cookie”, “Lightmap”, “Advanced” };

  28. //Max Size

  29. private int MaxSizeInt = 5;

  30. private string[] MaxSizeString = new string[] { “32”, “64”, “128”, “256”, “512”, “1024”, “2048”, “4096” };

  31. //Format

  32. private int FormatInt = 0;

  33. private string[] FormatString = new string[] { “Compressed”, “16 bits”, “true color” };

  34. /// <summary>

  35. /// 创建、显示窗体

  36. /// </summary>

  37. [@MenuItem(“DuanMenu/Texture Import Settings”)]

  38. private static void Init()

  39. {

  40. TextureImportSetting window = (TextureImportSetting)EditorWindow.GetWindow(typeof(TextureImportSetting), true, “TextureImportSetting”);

  41. window.Show();

  42. }

  43. /// <summary>

  44. /// 显示窗体里面的内容

  45. /// </summary>

  46. private void OnGUI()

  47. {

  48. //AnisoLevel

  49. GUILayout.BeginHorizontal();

  50. GUILayout.Label(“Aniso Level  ”);

  51. AnisoLevel = EditorGUILayout.IntSlider(AnisoLevel, 0, 9);

  52. GUILayout.EndHorizontal();

  53. //Filter Mode

  54. FilterModeInt = EditorGUILayout.IntPopup(“Filter Mode”, FilterModeInt, FilterModeString, IntArray);

  55. //Wrap Mode

  56. WrapModeInt = EditorGUILayout.IntPopup(“Wrap Mode”, WrapModeInt, WrapModeString, IntArray);

  57. //Texture Type

  58. TextureTypeInt = EditorGUILayout.IntPopup(“Texture Type”, TextureTypeInt, TextureTypeString, IntArray);

  59. //Max Size

  60. MaxSizeInt = EditorGUILayout.IntPopup(“Max Size”, MaxSizeInt, MaxSizeString, IntArray);

  61. //Format

  62. FormatInt = EditorGUILayout.IntPopup(“Format”, FormatInt, FormatString, IntArray);

  63. if (GUILayout.Button(“Set Texture ImportSettings”))

  64. LoopSetTexture();

  65. }

  66. /// <summary>

  67. /// 获取贴图设置

  68. /// </summary>

  69. public TextureImporter GetTextureSettings(string path)

  70. {

  71. TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

  72. //AnisoLevel

  73. textureImporter.anisoLevel = AnisoLevel;

  74. //Filter Mode

  75. switch (FilterModeInt)

  76. {

  77. case 0:

  78. textureImporter.filterMode = FilterMode.Point;

  79. break;

  80. case 1:

  81. textureImporter.filterMode = FilterMode.Bilinear;

  82. break;

  83. case 2:

  84. textureImporter.filterMode = FilterMode.Trilinear;

  85. break;

  86. }

  87. //Wrap Mode

  88. switch (WrapModeInt)

  89. {

  90. case 0:

  91. textureImporter.wrapMode = TextureWrapMode.Repeat;

  92. break;

  93. case 1:

  94. textureImporter.wrapMode = TextureWrapMode.Clamp;

  95. break;

  96. }

  97. //Texture Type

  98. switch (TextureTypeInt)

  99. {

  100. case 0:

  101. textureImporter.textureType = TextureImporterType.Image;

  102. break;

  103. case 1:

  104. textureImporter.textureType = TextureImporterType.Bump;

  105. break;

  106. case 2:

  107. textureImporter.textureType = TextureImporterType.GUI;

  108. break;

  109. case 3:

  110. textureImporter.textureType = TextureImporterType.Reflection;

  111. break;

  112. case 4:

  113. textureImporter.textureType = TextureImporterType.Cookie;

  114. break;

  115. case 5:

  116. textureImporter.textureType = TextureImporterType.Lightmap;

  117. break;

  118. case 6:

  119. textureImporter.textureType = TextureImporterType.Advanced;

  120. break;

  121. }

  122. //Max Size

  123. switch (MaxSizeInt)

  124. {

  125. case 0:

  126. textureImporter.maxTextureSize = 32;

  127. break;

  128. case 1:

  129. textureImporter.maxTextureSize = 64;

  130. break;

  131. case 2:

  132. textureImporter.maxTextureSize = 128;

  133. break;

  134. case 3:

  135. textureImporter.maxTextureSize = 256;

  136. break;

  137. case 4:

  138. textureImporter.maxTextureSize = 512;

  139. break;

  140. case 5:

  141. textureImporter.maxTextureSize = 1024;

  142. break;

  143. case 6:

  144. textureImporter.maxTextureSize = 2048;

  145. break;

  146. case 7:

  147. textureImporter.maxTextureSize = 4096;

  148. break;

  149. }

  150. //Format

  151. switch (FormatInt)

  152. {

  153. case 0:

  154. textureImporter.textureFormat = TextureImporterFormat.AutomaticCompressed;

  155. break;

  156. case 1:

  157. textureImporter.textureFormat = TextureImporterFormat.Automatic16bit;

  158. break;

  159. case 2:

  160. textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;

  161. break;

  162. }

  163. return textureImporter;

  164. }

  165. /// <summary>

  166. /// 循环设置选择的贴图

  167. /// </summary>

  168. private void LoopSetTexture()

  169. {

  170. Object[] textures = GetSelectedTextures();

  171. Selection.objects = new Object[0];

  172. foreach (Texture2D texture in textures)

  173. {

  174. string path = AssetDatabase.GetAssetPath(texture);

  175. TextureImporter texImporter = GetTextureSettings(path);

  176. TextureImporterSettings tis = new TextureImporterSettings();

  177. texImporter.ReadTextureSettings(tis);

  178. texImporter.SetTextureSettings(tis);

  179. AssetDatabase.ImportAsset(path);

  180. }

  181. }

  182. /// <summary>

  183. /// 获取选择的贴图

  184. /// </summary>

  185. /// <returns></returns>

  186. private Object[] GetSelectedTextures()

  187. {

  188. return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);

  189. }

  190. }
复制代码





欢迎光临 纳金网 (http://rs.narkii.com/club/) Powered by Discuz! X2.5