Unity 批量修改图片Format

要批量修改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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值