­

Unity3D设置纹理格式

1. 简介

    在PC上开发时,其天空盒的效果很好,但是为Android平台Build之后,其效果简直没法看。

    更为恼火的是,之后PC上的纹理效果也变差了,新加入的纹理效果都会变差,看其纹理格式,使用ETC进行了压缩。

2. Unity3D默认纹理格式问题

2.1 在导入时是否自动压缩

     Edit->Preferences…

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

fileFormatVersion: 2  guid: e658bdd655d56c64eb2bf011d186cded  TextureImporter:    serializedVersion: 2    mipmaps:      mipMapMode: 0      enableMipMap: 1      linearTexture: 0      correctGamma: 0      fadeOut: 0      borderMipMap: 0      mipMapFadeDistanceStart: 1      mipMapFadeDistanceEnd: 3    bumpmap:      convertToNormalMap: 0      externalNormalMap: 0      heightScale: .25      normalMapFilter: 0    isReadable: 0    grayScaleToAlpha: 0    generateCubemap: 0    seamlessCubemap: 0    textureFormat: 4    maxTextureSize: 1024    textureSettings:      filterMode: -1      aniso: -1      mipBias: -1      wrapMode: -1    nPOTScale: 1    lightmap: 0    compressionQuality: 50    spriteMode: 0    spriteExtrude: 1    spriteMeshType: 1    alignment: 0    spritePivot: {x: .5, y: .5}    spriteBorder: {x: 0, y: 0, z: 0, w: 0}    spritePixelsToUnits: 100    alphaIsTransparency: 0    textureType: -1    buildTargetSettings: []    spriteSheet:      sprites: []    spritePackingTag:     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内容如下:

using UnityEngine;    using UnityEditor;        // /////////////////////////////////////////////////////////////////////////////////////////////////////////  //  // Batch Texture import settings modifier.  //  // Modifies all selected textures in the project window and applies the requested modification on the    // textures. Idea was to have the same choices for multiple files as you would have if you open the    // import settings of a single texture. Put this into Assets/Editor and once compiled by Unity you find    // the new functionality in Custom -> Texture. Enjoy! :-)    //    // Based on the great work of benblo in this thread:  // http://forum.unity3d.com/viewtopic.php?t=16079&start=0&postdays=0&postorder=asc&highlight=textureimporter  //  // Developed by Martin Schultz, Decane in August 2009  // e-mail: ms@decane.net  //  // Updated for Unity 3.0 by col000r in August 2010  // http://col000r.blogspot.com  //  // /////////////////////////////////////////////////////////////////////////////////////////////////////////    public class ChangeTextureImportSettingsUnity3 : ScriptableObject {        	[MenuItem ("Custom/Texture/Change Texture Format/Auto Compressed")]    	static void ChangeTextureFormat_AutoCompressed() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.AutomaticCompressed);    	}        	[MenuItem ("Custom/Texture/Change Texture Format/Auto 16bit")]    	static void ChangeTextureFormat_Auto16Bit() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.Automatic16bit);    	}        	[MenuItem ("Custom/Texture/Change Texture Format/Auto Truecolor")]    	static void ChangeTextureFormat_AutoTruecolor() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.AutomaticTruecolor);    	}        	[MenuItem ("Custom/Texture/Change Texture Format/RGB Compressed DXT1")]    	static void ChangeTextureFormat_RGB_DXT1() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT1);    	}        	[MenuItem ("Custom/Texture/Change Texture Format/RGB Compressed DXT5")]    	static void ChangeTextureFormat_RGB_DXT5() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.DXT5);    	}        	[MenuItem ("Custom/Texture/Change Texture Format/RGB 16 bit")]    	static void ChangeTextureFormat_RGB_16bit() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB16);    	}        	[MenuItem ("Custom/Texture/Change Texture Format/RGB 24 bit")]    	static void ChangeTextureFormat_RGB_24bit() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.RGB24);    	}        	[MenuItem ("Custom/Texture/Change Texture Format/Alpha 8 bit")]    	static void ChangeTextureFormat_Alpha_8bit() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.Alpha8);    	}        	[MenuItem ("Custom/Texture/Change Texture Format/ARGB 16 bit")]    	static void ChangeTextureFormat_RGBA_16bit() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB16);    	}        	[MenuItem ("Custom/Texture/Change Texture Format/RGBA 32 bit")]    	static void ChangeTextureFormat_RGBA_32bit() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.RGBA32);    	}        	[MenuItem ("Custom/Texture/Change Texture Format/ARGB 32 bit")]    	static void ChangeTextureFormat_ARGB_32bit() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.ARGB32);    	}        	[MenuItem ("Custom/Texture/Change Texture Format/RGB PVRTC 2bit")]    	static void ChangeTextureFormat_RGB_PVRTC_2bit() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGB2);    	}        	[MenuItem ("Custom/Texture/Change Texture Format/RGBA PVRTC 2bit")]    	static void ChangeTextureFormat_RGBA_PVRTC_2bit() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGBA2);    	}            	[MenuItem ("Custom/Texture/Change Texture Format/RGB PVRTC 4bit")]    	static void ChangeTextureFormat_RGB_PVRTC_4bit() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGB4);    	}        	[MenuItem ("Custom/Texture/Change Texture Format/RGBA PVRTC 4bit")]    	static void ChangeTextureFormat_RGBA_PVRTC_4bit() {    		SelectedChangeTextureFormatSettings(TextureImporterFormat.PVRTC_RGBA4);    	}        	// ----------------------------------------------------------------------------        	[MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/32")]    	static void ChangeTextureSize_32() {    		SelectedChangeMaxTextureSize(32);    	}        	[MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/64")]    	static void ChangeTextureSize_64() {    		SelectedChangeMaxTextureSize(64);    	}        	[MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/128")]    	static void ChangeTextureSize_128() {    		SelectedChangeMaxTextureSize(128);    	}        	[MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/256")]    	static void ChangeTextureSize_256() {    		SelectedChangeMaxTextureSize(256);    	}        	[MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/512")]    	static void ChangeTextureSize_512() {    		SelectedChangeMaxTextureSize(512);    	}        	[MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/1024")]    	static void ChangeTextureSize_1024() {    		SelectedChangeMaxTextureSize(1024);    	}        	[MenuItem ("Custom/Texture/Change Texture Size/Change Max Texture Size/2048")]    	static void ChangeTextureSize_2048() {    		SelectedChangeMaxTextureSize(2048);    	}        	// ----------------------------------------------------------------------------        	[MenuItem ("Custom/Texture/Change MipMap/Enable MipMap")]    	static void ChangeMipMap_On() {    		SelectedChangeMimMap(true);    	}        	[MenuItem ("Custom/Texture/Change MipMap/Disable MipMap")]    	static void ChangeMipMap_Off() {    		SelectedChangeMimMap(false);    	}        	// ----------------------------------------------------------------------------            	[MenuItem ("Custom/Texture/Change Non Power of 2/None")]    	static void ChangeNPOT_None() {    		SelectedChangeNonPowerOf2(TextureImporterNPOTScale.None);    	}        	[MenuItem ("Custom/Texture/Change Non Power of 2/ToNearest")]    	static void ChangeNPOT_ToNearest() {    		SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToNearest);    	}        	[MenuItem ("Custom/Texture/Change Non Power of 2/ToLarger")]    	static void ChangeNPOT_ToLarger() {    		SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToLarger);    	}        	[MenuItem ("Custom/Texture/Change Non Power of 2/ToSmaller")]    	static void ChangeNPOT_ToSmaller() {    		SelectedChangeNonPowerOf2(TextureImporterNPOTScale.ToSmaller);    	}            	// ----------------------------------------------------------------------------        	[MenuItem ("Custom/Texture/Change Is Readable/Enable")]    	static void ChangeIsReadable_Yes() {    		SelectedChangeIsReadable(true);    	}        	[MenuItem ("Custom/Texture/Change Is Readable/Disable")]    	static void ChangeIsReadable_No() {    		SelectedChangeIsReadable(false);    	}    //Unity3D教程手册:www.unitymanual.com        	// ----------------------------------------------------------------------------        	static void SelectedChangeIsReadable(bool enabled) {        		Object[] textures = GetSelectedTextures();    		Selection.objects = new Object[0];    		foreach (Texture2D texture in textures)  {    			string path = AssetDatabase.GetAssetPath(texture);    			TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;    			textureImporter.isReadable = enabled;        			AssetDatabase.ImportAsset(path);    		}    	}            	static void SelectedChangeNonPowerOf2(TextureImporterNPOTScale npot) {        		Object[] textures = GetSelectedTextures();    		Selection.objects = new Object[0];    		foreach (Texture2D texture in textures)  {    			string path = AssetDatabase.GetAssetPath(texture);    			TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;    			textureImporter.npotScale = npot;        			AssetDatabase.ImportAsset(path);    		}    	}        	static void SelectedChangeMimMap(bool enabled) {        		Object[] textures = GetSelectedTextures();    		Selection.objects = new Object[0];    		foreach (Texture2D texture in textures)  {    			string path = AssetDatabase.GetAssetPath(texture);    			TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;    			textureImporter.mipmapEnabled = enabled;        			AssetDatabase.ImportAsset(path);    		}    	}    	//Unity3D教程手册:www.unitymanual.com    	static void SelectedChangeMaxTextureSize(int size) {        		Object[] textures = GetSelectedTextures();    		Selection.objects = new Object[0];    		foreach (Texture2D texture in textures)  {    			string path = AssetDatabase.GetAssetPath(texture);    			TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;    			textureImporter.maxTextureSize = size;      			AssetDatabase.ImportAsset(path);    		}    	}        	static void SelectedChangeTextureFormatSettings(TextureImporterFormat newFormat) {        		Object[] textures = GetSelectedTextures();    		Selection.objects = new Object[0];    		foreach (Texture2D texture in textures)  {    			string path = AssetDatabase.GetAssetPath(texture);    			//Debug.Log("path: " + path);    			TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;    			textureImporter.textureFormat = newFormat;      			AssetDatabase.ImportAsset(path);    		}    	}        	static Object[] GetSelectedTextures()    	{    		return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);    	}    }

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

然后,Unity3D界面如下:

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