unity导入纹理时,会设置Texture Type:Texture,特点是长宽均为2的幂,导致尺寸与原始图片不同,这就需要我们手动修改纹理的Texture Type,或在代码里修改。类似代码如下:
static void ChangeTextureImporter(Texture2D texture)
{
string path = AssetDatabase.GetAssetPath(texture);
TextureImporter texImporter = GetTextureSettings(path);
TextureImporterSettings tis = new TextureImporterSettings();
texImporter.ReadTextureSettings(tis);
tis.ApplyTextureType(TextureImporterType.GUI, false);
texImporter.SetTextureSettings(tis);
AssetDatabase.ImportAsset(path);
}
static TextureImporter GetTextureSettings(string path)
{
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
textureImporter.textureType = TextureImporterType.GUI;
textureImporter.npotScale = TextureImporterNPOTScale.ToNearest;
return textureImporter;
}
它修改Texture Type为GUI,保证了原始图片的尺寸,同时Apply该设置。