最近在做項目的過程中有操作圖片導入 Unity工程後修改圖片格式的一些功能,比如(圖片壓縮格式,Alpha通道信息,圖片可讀可寫等),一看到這個功能,就不假思索的開始使用了AssetPostprocessor的OnPostprocessTexture方法來實現。
void OnPostprocessTexture(Texture2D texture2D)
{
var importer = assetImporter as TextureImporter;
importer.alphaSource = TextureImporterAlphaSource.FromInput;
importer.alphaIsTransparency = true;
importer.isReadable = true;
importer.textureCompression = TextureImporterCompression.Uncompressed;
importer.textureType = TextureImporterType.Sprite;
}
這樣寫完之後,發現在項目中圖片格式並未真正的修改了,之後查看Unity的api文檔,終於發現了問題的根源。
原來修改圖片的壓縮格式必須放在OnPreprocessTexture方法中執行纔能有效。
上圖
另外,如果有對圖片像素信息的修改,請一定要在OnPreprocessTexture中設置圖片的可讀可寫屬性,這樣才能在運行時操作圖片。
————————————————
版權聲明:本文爲CSDN博主「FocusGame」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qizi329/article/details/85916842