要批量修改Ios端的图片压缩格式 这里记录一下
public void SetTextureToASTC(string path)
{
string[] childPaths = Directory.GetDirectories(path);
foreach (string childPath in childPaths)
{
string tempPath = childPath.Replace("\\", "/");
tempPath = tempPath.Substring(tempPath.IndexOf("Assets"));
if (ignoreFolder.Contains(tempPath))
{
continue;
}
SetTextureToASTC(childPath);
}
string[] filePaths = Directory.GetFiles(path);
foreach (string filePath in filePaths)
{
if (!filePath.Contains(".meta") && (filePath.Contains(".png") || filePath.Contains(".jpg")))
{
string assetPath = filePath.Substring(filePath.IndexOf("/Assets") + 1);
importer = TextureImporter.GetAtPath(assetPath) as TextureImporter;
if (importer.textureType != TextureImporterType.Sprite)
continue;
if (importer == null)
{
Debug.LogError($"没有获取到{assetPath}的TextureImporter");
continue;
}
platSetting = importer.GetPlatformTextureSettings(BuildTarget.iOS.ToString());
if (platSetting.format.ToString().Contains("ASTC"))
continue;
if (!platSetting.overridden)
platSetting.overridden = true;
texture = AssetDatabase.LoadAssetAtPath<Texture2D>(assetPath);
//判断图片是否导入Alpha通道
if (importer.alphaSource == TextureImporterAlphaSource.None)
{
//先判断8 6 再判断4
if (MatchTimes(8, texture))
{
platSetting.format = TextureImporterFormat.ASTC_RGB_8x8;
}
else if (MatchTimes(6, texture))
{
platSetting.format = TextureImporterFormat.ASTC_RGB_6x6;
}
else if (MatchTimes(4, texture))
{
platSetting.format = TextureImporterFormat.ASTC_RGB_4x4;
}
else
{
Debug.LogError(assetPath + " 该图片没有符合的倍率");
}
}
else
{
//先判断8 6 再判断4
if (MatchTimes(8, texture))
{
platSetting.format = TextureImporterFormat.ASTC_RGBA_8x8;
}
else if (MatchTimes(6, texture))
{
platSetting.format = TextureImporterFormat.ASTC_RGBA_6x6;
}
else if (MatchTimes(4, texture))
{
platSetting.format = TextureImporterFormat.ASTC_RGBA_4x4;
}
else
{
Debug.LogError(assetPath + " 该图片没有符合的倍率");
}
}
importer.SetPlatformTextureSettings(platSetting);
importer.SaveAndReimport();
}
}
}
private bool MatchTimes(int times, Texture2D texture)
{
return (texture.width % times) == 0 && (texture.height % times) == 0;
}

3323

被折叠的 条评论
为什么被折叠?



