Unity3D設定紋理格式
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
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: e658bdd655d56c64eb2bf011d186cdedTextureImporter: 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: [email protected]//// 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視窗中選中需要設定的紋理(可多選),然後點選單命令執行對應的轉換即可。