using UnityEngine;
using UnityEditor;
using System.IO;
//需要继承AssetPostprocessor
public class SetTextureType: AssetPostprocessor {
void OnPostprocessTexture(Texture2D texture) {
//资源目录
string dir = Path.GetDirectoryName(assetPath);
//判断资源是否在目录中
if(dir.Contains( "Assets" )) {
FileStream fs = File.OpenRead(assetPath); int filelength = 0;
//获得文件长度
filelength = (int)fs.Length;
//建立一个字节数组
byte[] image = new byte[filelength];
//按字节流读取
fs.Read(image, 0, filelength);
//引用System.Drawing
System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
fs.Close();
Debug.Log(result.Width + " * " + result.Height);
//判断最大尺寸
System.Func<int, int> maxValue = (value) => {
int temp = 1;
int index = 0;
while (true) {
if (Mathf.Pow(2, temp) >= value)
return (int)Mathf.Pow(2, temp);
temp++;
index++;
if (index >= 10) {
Debug.Log("超出尺寸" + value); return 4096; }
}
};
int maxSize = maxValue(Mathf.Max(result.Width, result.Height));
TextureImporter textureImporter = assetImporter as TextureImporter;
//判断是否为png
if (assetPath.Substring(assetPath.Length - 3) == "png") {
Debug.Log(assetPath);
//如果图片格式为Image类型,修改为Advanced
if (textureImporter.textureType == TextureImporterType.Image)
textureImporter.textureType = TextureImporterType.Advanced;
else return;
//textureImporter.spritePackingTag = "";
textureImporter.npotScale = TextureImporterNPOTScale.None;
textureImporter.textureFormat = TextureImporterFormat.AutomaticCompressed;
textureImporter.spriteImportMode = SpriteImportMode.Single;
//if(textureImporter.mipmapEnabled )
//textureImporter.spriteImportMode = SpriteImportMode.Multiple;
//Debug.Log(textureImporter.spritesheet[0].rect);
textureImporter.mipmapEnabled = false;
textureImporter.compressionQuality = 100;
//单独设置iPhone平台参数:参数名称,最大尺寸,Format类型,压缩比率,(是否分割成RGB +纹理,ETC1压缩能)
textureImporter.SetPlatformTextureSettings("iPhone", maxSize, TextureImporterFormat.ASTC_RGB_4x4, 50, true);
} else {
if (textureImporter.textureType == TextureImporterType.Image)
textureImporter.textureType = TextureImporterType.Sprite; else return;
}
//是否开启透明通道
textureImporter.alphaIsTransparency = true;
}
}
}
不同平台的名称:官方连接
"Standalone", "iPhone", "Android", "WebGL", "Windows Store Apps", "PSP2", "PS4", "XboxOne", "Nintendo 3DS", "WiiU", "tvOS".
(苹果的设置是iPhone,居然不是iOS)