Texture ASTC转换ETC

传统方式修改是通过TextureImporter的方法,先把Texture加载出来再修改,但是这样的话很浪费时间;我的方法是直接修改Texture的.meta文件,速度比之前的快一倍。

[MenuItem("Assets/设置文件夹以及子文件夹下面的图片压缩格式为ETC", priority = 0)]
    static void AutoSetASTC()
    {
        Debug.Log("开始");
        string[] guid = Selection.assetGUIDs;
        List<string> lstPath = new List<string>();
        for (int i = 0; i < guid.Length; i++)
        {
            string dirPath = AssetDatabase.GUIDToAssetPath(guid[i]);
            lstPath.Add(dirPath);
        }

        string[] textures = AssetDatabase.FindAssets("t:Texture", lstPath.ToArray());
        for (int i = 0; i < textures.Length; i++)
        {
            EditorUtility.DisplayProgressBar("Texture", i + "/" + textures.Length, (float)i / textures.Length);

            //修改Meta文件
            bool bModify = false;
            string texPath = AssetDatabase.GUIDToAssetPath(textures[i]);
            TextureImporter texImporter = AssetImporter.GetAtPath(texPath) as TextureImporter;
            string metaPath = texPath + ".meta";
            List<string> metaContent = new List<string>();
            using (StreamReader sr = new StreamReader(metaPath))
            {
                bool bAndroid = false;
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine();
                    bAndroid = line.Contains("buildTarget") ? (line.Contains("Android") ? true : false) : bAndroid;

                    if (bAndroid && line.IndexOf("textureFormat:") != -1)
                    {
                        string[] textureMetas = line.Split(':');
                        if (textureMetas.Length == 2)
                        {
                            int texFormat = int.Parse(textureMetas[1]);
                            if (texFormat >= (int)TextureImporterFormat.ASTC_RGB_4x4 && texFormat <= (int)TextureImporterFormat.ASTC_RGB_12x12)
                            {
                                bModify = true;
                                texFormat = (int)TextureImporterFormat.ETC2_RGB4;
                                line = string.Format("{0}: {1}", textureMetas[0], texFormat.ToString());
                            }
                            else if (texFormat >= (int)TextureImporterFormat.ASTC_RGBA_4x4 && texFormat <= (int)TextureImporterFormat.ASTC_RGBA_12x12)
                            {
                                bModify = true;
                                texFormat = (int)TextureImporterFormat.ETC2_RGBA8;
                                line = string.Format("{0}: {1}", textureMetas[0], texFormat.ToString());
                            }
                        }
                    }

                    if (bAndroid && line.IndexOf("overridden:") != -1)
                    {
                        string[] textureMetas = line.Split(':');
                        if (textureMetas.Length == 2)
                        {
                            int texFormat = int.Parse(textureMetas[1]);
                            if (bModify)
                                bModify = texFormat == 1 ? true : false;
                        }
                    }

                    metaContent.Add(line);
                }
                sr.Close();
            }

            if (bModify)
            {
                File.Delete(metaPath);
                using (StreamWriter sw = new StreamWriter(metaPath))
                {
                    foreach (string line in metaContent)
                    {
                        sw.WriteLine(line);
                    }
                    sw.Close();
                }
            }
            metaContent.Clear();

            //对Texture进行修改
            //int maxTexSize;
            //int compressionQuality;
            //TextureImporterFormat texFormat;
            //string texPath = AssetDatabase.GUIDToAssetPath(textures[i]);
            //TextureImporter texImporter = AssetImporter.GetAtPath(texPath) as TextureImporter;
            //TextureImporterPlatformSettings settingPlatform = texImporter.GetPlatformTextureSettings("Android");

            //if (texImporter.GetPlatformTextureSettings("Android", out maxTexSize, out texFormat, out compressionQuality))
            //{
            //    //texFormat = texImporter.DoesSourceTextureHaveAlpha() ? (texFormat != TextureImporterFormat.ETC2_RGBA8 ? TextureImporterFormat.ETC2_RGBA8 : texFormat)
            //    //    : (texFormat != TextureImporterFormat.ETC2_RGB4 ? TextureImporterFormat.ETC2_RGB4 : texFormat);
            //    //settingPlatform.format = texFormat;

            //    texFormat = texImporter.DoesSourceTextureHaveAlpha() ? (texFormat != TextureImporterFormat.ASTC_RGBA_8x8 ? TextureImporterFormat.ASTC_RGBA_8x8 : texFormat)
            //            : (texFormat != TextureImporterFormat.ASTC_RGB_8x8 ? TextureImporterFormat.ASTC_RGB_8x8 : texFormat);
            //    settingPlatform.format = texFormat;
            //}
            //texImporter.SetPlatformTextureSettings(settingPlatform);
            //texImporter.SaveAndReimport();
        }
        AssetDatabase.Refresh();
        Debug.Log("End");

        EditorUtility.ClearProgressBar();
    }

 

直接拷贝就能用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值