當(dāng)我們將圖片導(dǎo)入到工程中時(shí)窟感,unity 3d會(huì)對(duì)圖片進(jìn)行處理吓著,設(shè)置圖片的默認(rèn)格式鲤嫡。如下圖所示,是圖片導(dǎo)入到工程中绑莺,unity 3d進(jìn)行的默認(rèn)設(shè)置暖眼。
但是,一般情況下纺裁,這樣的圖片格式并不能滿足我們的需求罢荡。所以我就想,有沒(méi)有辦法修改圖片導(dǎo)入時(shí)設(shè)置的格式呢对扶?答案是有的区赵。
首先說(shuō)一下思路:
1. 我們可以在Assets文件夾下建立一個(gè)文件夾,例如:GameResources浪南。只有當(dāng)將圖片導(dǎo)入GameResources文件夾內(nèi)時(shí)笼才,才會(huì)對(duì)圖片進(jìn)行處理。
2. 有些圖片需要打包到圖集中络凿。那么骡送,我們可以在GameResources下面建立以圖集名命名的文件夾,例如:UI絮记。UI文件夾下的圖片的圖集都會(huì)被設(shè)置為UI摔踱。
3. 有時(shí)我們的工程中不希望導(dǎo)入某些類(lèi)型的圖片,例如.dds怨愤。那么可以對(duì)該類(lèi)型的圖片進(jìn)行過(guò)濾派敷。
代碼如下:
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Reflection;
public class ImportSetting : AssetPostprocessor
{
static int[] maxSizes = { 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
static readonly string DEFAULTS_KEY = "DEFAULTS_DONE";
static readonly uint DEFAULTS_VERSION = 2;
public bool IsAssetProcessed
{
get
{
string key = string.Format("{0}_{1}", DEFAULTS_KEY, DEFAULTS_VERSION);
return assetImporter.userData.Contains(key);
}
set
{
string key = string.Format("{0}_{1}", DEFAULTS_KEY, DEFAULTS_VERSION);
assetImporter.userData = value ? key : string.Empty;
}
}
void OnPreprocessTexture()
{
if (IsAssetProcessed)
return;
IsAssetProcessed = true;
if (assetPath.IndexOf("GameResource") < 0 || assetPath.IndexOf("@e") >= 0)
return;
TextureImporter textureImporter = (TextureImporter)assetImporter;
if (textureImporter == null)
return;
textureImporter.textureType = TextureImporterType.Advanced;
int width = 0;
int height = 0;
GetOriginalSize(textureImporter, out width, out height);
if (!GetIsMultipleOf4(width) || !GetIsMultipleOf4(height))
{
Debug.LogError("4---" + assetPath);
}
bool IsPowerOfTwo = GetIsPowerOfTwo(width) && GetIsPowerOfTwo(height);
if (!IsPowerOfTwo)
textureImporter.npotScale = TextureImporterNPOTScale.None;
textureImporter.mipmapEnabled = false;
textureImporter.maxTextureSize = GetMaxSize(Mathf.Max(width,height));
if (assetPath.EndsWith(".jpg"))
{
textureImporter.textureFormat = TextureImporterFormat.ETC2_RGB4;
}
else if (assetPath.EndsWith(".png"))
{
if (!textureImporter.DoesSourceTextureHaveAlpha())
textureImporter.grayscaleToAlpha = true;
textureImporter.alphaIsTransparency = true;
textureImporter.textureFormat = TextureImporterFormat.ETC2_RGBA8;
string atlasName = new System.IO.DirectoryInfo(System.IO.Path.GetDirectoryName(assetPath)).Name;
if (atlasName.Equals("UI"))
{
textureImporter.spriteImportMode = SpriteImportMode.Single;
textureImporter.spritePackingTag = "UI";
}
}
else
{
Debug.Log("圖片格式---"+assetPath);
}
}
/// <summary>
/// 獲取texture的原始文件尺寸
/// </summary>
/// <param name="importer"></param>
/// <param name="width"></param>
/// <param name="height"></param>
void GetOriginalSize(TextureImporter importer, out int width, out int height)
{
object[] args = new object[2] { 0, 0 };
MethodInfo mi = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(importer, args);
width = (int)args[0];
height = (int)args[1];
}
bool GetIsMultipleOf4(int f)
{
return f % 4 == 0;
}
bool GetIsPowerOfTwo(int f)
{
return (f & (f - 1)) == 0;
}
int GetMaxSize(int longerSize)
{
int index = 0;
for (int i = 0; i < maxSizes.Length; i++)
{
if (longerSize <= maxSizes[i])
{
index = i;
break;
}
}
return maxSizes[index];
}
}
**有幾點(diǎn)需要說(shuō)明一下:**
1. 在實(shí)際的使用中發(fā)現(xiàn),OnPreprocessTexture()方法在每次更改圖片格式的時(shí)候都會(huì)執(zhí)行,結(jié)果就是我們?cè)趯?dǎo)入圖片后就無(wú)法更改圖片的格式了篮愉。所以我們使用IsAssetProcessed屬性來(lái)記錄是否在導(dǎo)入時(shí)對(duì)圖片進(jìn)行了設(shè)置腐芍。一旦設(shè)置過(guò)了,就不會(huì)對(duì)圖片進(jìn)行處理了试躏。
2. 最麻煩的就是獲取圖片的原始尺寸猪勇。還是在國(guó)外的論壇發(fā)現(xiàn)的方法,就是GetOriginalSize()方法颠蕴,用來(lái)獲取圖片的原始尺寸泣刹。
工程地址:https://github.com/bzyzhang/TextureTool