Unity图片格式转换

//转自 http://blog.csdn.net/myarrow/article/details/43017487
Unity3D默认纹理格式问题

2.1 在导入时是否自动压缩

 Edit->Preferences...

当选择此选项之后,每当导入新的纹理(无论是拖入或在文件管理器中copy),Unity3D都会根据当前平台的设置进行自动转换,此纹理转换,并不是把纹理文件进行修改,纹理文件是不动的,而是增加了一个.meta文件(如Sunny4_back.tif对应Sunny4_back.tif.meta),其中定义了很多与纹理相关的参数,其中决定此纹理格式的参数为:textureType,此文件内容如下:

[javascript] view plain copy

在CODE上查看代码片派生到我的代码片01.fileFormatVersion: 2
02.guid: e658bdd655d56c64eb2bf011d186cded
03.TextureImporter:
04. serializedVersion: 2
05. mipmaps:
06. mipMapMode: 0
07. enableMipMap: 1
08. linearTexture: 0
09. correctGamma: 0
10. fadeOut: 0
11. borderMipMap: 0
12. mipMapFadeDistanceStart: 1
13. mipMapFadeDistanceEnd: 3
14. bumpmap:
15. convertToNormalMap: 0
16. externalNormalMap: 0
17. heightScale: .25
18. normalMapFilter: 0
19. isReadable: 0
20. grayScaleToAlpha: 0
21. generateCubemap: 0
22. seamlessCubemap: 0
23. textureFormat: 4
24. maxTextureSize: 1024
25. textureSettings:
26. filterMode: -1
27. aniso: -1
28. mipBias: -1
29. wrapMode: -1
30. nPOTScale: 1
31. lightmap: 0
32. compressionQuality: 50
33. spriteMode: 0
34. spriteExtrude: 1
35. spriteMeshType: 1
36. alignment: 0
37. spritePivot: {x: .5, y: .5}
38. spriteBorder: {x: 0, y: 0, z: 0, w: 0}
39. spritePixelsToUnits: 100
40. alphaIsTransparency: 0
41. textureType: -1
42. buildTargetSettings: []
43. spriteSheet:
44. sprites: []
45. spritePackingTag:
46. userData:

2.2 Unity3D设置纹理格式

1) 选中纹理,纹理的Inspector窗口如下图所示:

上图显示的为Default设置,若Android平台没有单独设置, 则此纹理在Anroid平台采用默认设置,若Android平台单独设置了,则采用Android平台设置的格式。Unity3D只能设置三种纹理格式:Compressed、16bits、Truecolor,若要设置其它纹理格式,则Unity3D无能为力。

2.3 在Unity3D中自定义设置纹理格式

   把ChangeTextureImportSettings.cs放于Assets/Editor目录下,ChangeTextureImportSettings.cs内容如下:

[javascript] view plain copy

在CODE上查看代码片派生到我的代码片01.using UnityEngine;
02.
03.using UnityEditor;
04.
05.
06.
07.// /
08.//
09.// Batch Texture import settings modifier.
10.//
11.// Modifies all selected textures in the project window and applies the requested modification on the
12.
13.// textures. Idea was to have the same choices for multiple files as you would have if you open the
14.
15.// import settings of a single texture. Put this into Assets/Editor and once compiled by Unity you find
16.
17.// the new functionality in Custom -> Texture. Enjoy! :-)
18.
19.//
20.
21.// Based on the great work of benblo in this thread:
22.// http://forum.unity3d.com/viewtopic.php?t=16079&start=0&postdays=0&postorder=asc&highlight=textureimporter
23.//
24.// Developed by Martin Schultz, Decane in August 2009
25.// e-mail: ms@decane.net
26.//
27.// Updated for Unity 3.0 by col000r in August 2010
28.// http://col000r.blogspot.com
29.//
30.// /
31.
32.public class ChangeTextureImportSettingsUnity3 : ScriptableObject {
33.
34.
35.
36. [MenuItem (“Custom/Texture/Change Texture Format/Auto Compressed”)]
37.
38. static void ChangeTextureFormat_AutoCompressed() {
39.
40. SelectedChangeTextureFormatSettings(TextureImporterFormat.AutomaticCompressed);
41.
42. }
43.
44.
45.
46. [MenuItem (“Custom/Texture/Change Texture Format/Auto 16bit”)]
47.
48. static void ChangeTextureFormat_Auto16Bit() {
49.
50. SelectedChangeTextureFormatSettings(TextureImporterFormat.Automatic16bit);
51.
52. }
53.
54.
55.
56. [MenuItem (“Custom/Texture/Change Texture Format/Auto Truecolor”)]
57.
58. static void ChangeTextureFormat_AutoTruecolor() {
59.
60. SelectedChangeTextureFormatSettings(TextureImporterFormat.AutomaticTruecolor);
61.
62. }
63.
64.
65.
66. [MenuItem (“Custom/Texture/Change Texture Format/RGB Compressed DXT1”)]
67.
68. static void ChangeTextureFormat_RGB_DXT1() {
69.
70. SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT1);
71.
72. }
73.
74.
75.
76. [MenuItem (“Custom/Texture/Change Texture Format/RGB Compressed DXT5”)]
77.
78. static void ChangeTextureFormat_RGB_DXT5() {
79.
80. SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT5);
81.
82. }
83.
84.
85.
86. [MenuItem (“Custom/Texture/Change Texture Format/RGB 16 bit”)]
87.
88. static void ChangeTextureFormat_RGB_16bit() {
89.
90. SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB16);
91.
92. }
93.
94.
95.
96. [MenuItem (“Custom/Texture/Change Texture Format/RGB 24 bit”)]
97.
98. static void ChangeTextureFormat_RGB_24bit() {
99.
100. SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB24);
101.
102. }
103.
104.
105.
106. [MenuItem (“Custom/Texture/Change Texture Format/Alpha 8 bit”)]
107.
108. static void ChangeTextureFormat_Alpha_8bit() {
109.
110. SelectedChangeTextureFormatSettings(TextureImporterFormat.Alpha8);
111.
112. }
113.
114.
115.
116. [MenuItem (“Custom/Texture/Change Texture Format/ARGB 16 bit”)]
117.
118. static void ChangeTextureFormat_RGBA_16bit() {
119.
120. SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB16);
121.
122. }
123.
124.
125.
126. [MenuItem (“Custom/Texture/Change Texture Format/RGBA 32 bit”)]
127.
128. static void ChangeTextureFormat_RGBA_32bit() {
129.
130. SelectedChangeTextureFormatSettings(TextureImporterFormat.RGBA32);
131.
132. }
133.
134.
135.
136. [MenuItem (“Custom/Texture/Change Texture Format/ARGB 32 bit”)]
137.
138. static void ChangeTextureFormat_ARGB_32bit() {
139.
140. SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB32);
141.
142. }
143.
144.
145.
146. [MenuItem (“Custom/Texture/Change Texture Format/RGB PVRTC 2bit”)]
147.
148. static void ChangeTextureFormat_RGB_PVRTC_2bit() {
149.
150. SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGB2);
151.
152. }
153.
154.
155.
156. [MenuItem (“Custom/Texture/Change Texture Format/RGBA PVRTC 2bit”)]
157.
158. static void ChangeTextureFormat_RGBA_PVRTC_2bit() {
159.
160. SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGBA2);
161.
162. }
163.
164.
165.
166. [MenuItem (“Custom/Texture/Change Texture Format/RGB PVRTC 4bit”)]
167.
168. static void ChangeTextureFormat_RGB_PVRTC_4bit() {
169.
170. SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGB4);
171.
172. }
173.
174.
175.
176. [MenuItem (“Custom/Texture/Change Texture Format/RGBA PVRTC 4bit”)]
177.
178. static void ChangeTextureFormat_RGBA_PVRTC_4bit() {
179.
180. SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGBA4);
181.
182. }
183.
184.
185.
186. // —————————————————————————-
187.
188.
189.
190. [MenuItem (“Custom/Texture/Change Texture Size/Change Max Texture Size/32”)]
191.
192. static void ChangeTextureSize_32() {
193.
194. SelectedChangeMaxTextureSize(32);
195.
196. }
197.
198.
199.
200. [MenuItem (“Custom/Texture/Change Texture Size/Change Max Texture Size/64”)]
201.
202. static void ChangeTextureSize_64() {
203.
204. SelectedChangeMaxTextureSize(64);
205.
206. }
207.
208.
209.
210. [MenuItem (“Custom/Texture/Change Texture Size/Change Max Texture Size/128”)]
211.
212. static void ChangeTextureSize_128() {
213.
214. SelectedChangeMaxTextureSize(128);
215.
216. }
217.
218.
219.
220. [MenuItem (“Custom/Texture/Change Texture Size/Change Max Texture Size/256”)]
221.
222. static void ChangeTextureSize_256() {
223.
224. SelectedChangeMaxTextureSize(256);
225.
226. }
227.
228.
229.
230. [MenuItem (“Custom/Texture/Change Texture Size/Change Max Texture Size/512”)]
231.
232. static void ChangeTextureSize_512() {
233.
234. SelectedChangeMaxTextureSize(512);
235.
236. }
237.
238.
239.
240. [MenuItem (“Custom/Texture/Change Texture Size/Change Max Texture Size/1024”)]
241.
242. static void ChangeTextureSize_1024() {
243.
244. SelectedChangeMaxTextureSize(1024);
245.
246. }
247.
248.
249.
250. [MenuItem (“Custom/Texture/Change Texture Size/Change Max Texture Size/2048”)]
251.
252. static void ChangeTextureSize_2048() {
253.
254. SelectedChangeMaxTextureSize(2048);
255.
256. }
257.
258.
259.
260. // —————————————————————————-
261.
262.
263.
264. [MenuItem (“Custom/Texture/Change MipMap/Enable MipMap”)]
265.
266. static void ChangeMipMap_On() {
267.
268. SelectedChangeMimMap(true);
269.
270. }
271.
272.
273.
274. [MenuItem (“Custom/Texture/Change MipMap/Disable MipMap”)]
275.
276. static void ChangeMipMap_Off() {
277.
278. SelectedChangeMimMap(false);
279.
280. }
281.
282.
283.
284. // —————————————————————————-
285.
286.
287.
288.
289.
290. [MenuItem (“Custom/Texture/Change Non Power of 2/None”)]
291.
292. static void ChangeNPOT_None() {
293.
294. SelectedChangeNonPowerOf2(TextureImporterNPOTScale.None);
295.
296. }
297.
298.
299.
300. [MenuItem (“Custom/Texture/Change Non Power of 2/ToNearest”)]
301.
302. static void ChangeNPOT_ToNearest() {
303.
304. SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToNearest);
305.
306. }
307.
308.
309.
310. [MenuItem (“Custom/Texture/Change Non Power of 2/ToLarger”)]
311.
312. static void ChangeNPOT_ToLarger() {
313.
314. SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToLarger);
315.
316. }
317.
318.
319.
320. [MenuItem (“Custom/Texture/Change Non Power of 2/ToSmaller”)]
321.
322. static void ChangeNPOT_ToSmaller() {
323.
324. SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToSmaller);
325.
326. }
327.
328.
329.
330. // —————————————————————————-
331.
332.
333.
334. [MenuItem (“Custom/Texture/Change Is Readable/Enable”)]
335.
336. static void ChangeIsReadable_Yes() {
337.
338. SelectedChangeIsReadable(true);
339.
340. }
341.
342.
343.
344. [MenuItem (“Custom/Texture/Change Is Readable/Disable”)]
345.
346. static void ChangeIsReadable_No() {
347.
348. SelectedChangeIsReadable(false);
349.
350. } //Unity3D教程手册:www.unitymanual.com
351.
352.
353.
354. // —————————————————————————-
355.
356.
357.
358. static void SelectedChangeIsReadable(bool enabled) {
359.
360.
361.
362. Object[] textures = GetSelectedTextures();
363.
364. Selection.objects = new Object[0];
365.
366. foreach (Texture2D texture in textures) {
367.
368. string path = AssetDatabase.GetAssetPath(texture);
369.
370. TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
371.
372. textureImporter.isReadable = enabled;
373.
374. AssetDatabase.ImportAsset(path);
375.
376. }
377.
378. }
379.
380.
381.
382.
383.
384. static void SelectedChangeNonPowerOf2(TextureImporterNPOTScale npot) {
385.
386.
387.
388. Object[] textures = GetSelectedTextures();
389.
390. Selection.objects = new Object[0];
391.
392. foreach (Texture2D texture in textures) {
393.
394. string path = AssetDatabase.GetAssetPath(texture);
395.
396. TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
397.
398. textureImporter.npotScale = npot;
399.
400. AssetDatabase.ImportAsset(path);
401.
402. }
403.
404. }
405.
406.
407.
408. static void SelectedChangeMimMap(bool enabled) {
409.
410.
411.
412. Object[] textures = GetSelectedTextures();
413.
414. Selection.objects = new Object[0];
415.
416. foreach (Texture2D texture in textures) {
417.
418. string path = AssetDatabase.GetAssetPath(texture);
419.
420. TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
421.
422. textureImporter.mipmapEnabled = enabled;
423.
424. AssetDatabase.ImportAsset(path);
425.
426. }
427.
428. }
429.
430. //Unity3D教程手册:www.unitymanual.com
431.
432. static void SelectedChangeMaxTextureSize(int size) {
433.
434.
435.
436. Object[] textures = GetSelectedTextures();
437.
438. Selection.objects = new Object[0];
439.
440. foreach (Texture2D texture in textures) {
441.
442. string path = AssetDatabase.GetAssetPath(texture);
443.
444. TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
445.
446. textureImporter.maxTextureSize = size;
447.
448. AssetDatabase.ImportAsset(path);
449.
450. }
451.
452. }
453.
454.
455.
456. static void SelectedChangeTextureFormatSettings(TextureImporterFormat newFormat) {
457.
458.
459.
460. Object[] textures = GetSelectedTextures();
461.
462. Selection.objects = new Object[0];
463.
464. foreach (Texture2D texture in textures) {
465.
466. string path = AssetDatabase.GetAssetPath(texture);
467.
468. //Debug.Log(“path: ” + path);
469.
470. TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
471.
472. textureImporter.textureFormat = newFormat;
473.
474. AssetDatabase.ImportAsset(path);
475.
476. }
477.
478. }
479.
480.
481.
482. static Object[] GetSelectedTextures()
483.
484. {
485.
486. return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
487.
488. }
489.
490.}

 以上代码参考:Unity3d:批量修改贴图导入设置工具脚本 

然后,Unity3D界面如下:

在Project窗口中选中需要设置的纹理(可多选),然后点菜单命令执行对应的转换即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值