Unity 批量设置图片的纹理格式

using System.IO;
using UnityEditor;
using UnityEngine;

public class ChangeTextureData
{
    private static string[] paths = new string[]
    {
        Application.dataPath + "/Res/ab/base/image",
    };
    [MenuItem("Tools/ChangeTexture")]
    static void Change()
    {
        for (int i = 0; i < paths.Length; i++)
        {
            Pack(paths[i]);
        }
        AssetDatabase.Refresh();
    }

    private static void Pack(string path)
    {
        DirectoryInfo infos = new DirectoryInfo(path);
        FileSystemInfo[] files = infos.GetFileSystemInfos();
        for (int i = 0; i < files.Length; i++)
        {
            if (files[i] is DirectoryInfo)
            {
                Pack(files[i].FullName);
            }
            else
            {
                if (files[i].FullName.EndsWith(".png") || files[i].FullName.EndsWith(".jpg"))
                {
                    ChangeTexture(files[i].FullName, files[i].Name);
                }
            }
        }
    }

    static void ChangeTexture(string path,string imgname)
    {
        string[] strs1 = path.Split('\\');
        string temppath = "";
        for (int i = 0; i < strs1.Length-1; i++)
        {
            temppath = temppath + strs1[i] + @"\";
        }
        string _source = Replace(temppath);
        _source = _source + imgname;
        string _assetPath1 = "Assets" + _source.Substring(Application.dataPath.Length);
        TextureImporter importer = AssetImporter.GetAtPath(_assetPath1) as TextureImporter;
        Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(_assetPath1);
        int textureSize = Mathf.Max(texture.height, texture.width);
        int SizeType = FitSize(textureSize);

        #region 设置纹理方式一
        TextureImporterFormat and1 = TextureImporterFormat.ETC2_RGBA8;
        TextureImporterFormat and2 = TextureImporterFormat.ETC2_RGB4;
        TextureImporterFormat ios1 = TextureImporterFormat.ASTC_6x6;
        TextureImporterFormat ios2 = TextureImporterFormat.ASTC_6x6;
        TextureImporterPlatformSettings and = importer.GetPlatformTextureSettings("Android");
        and.overridden = true;
        and.maxTextureSize = SizeType;
        TextureImporterPlatformSettings ios = importer.GetPlatformTextureSettings("iPhone");
        ios.overridden = true;
        ios.maxTextureSize = SizeType;
        if (importer.DoesSourceTextureHaveAlpha())
        {
            and.format = and1;
            ios.format = ios1;
        }
        else
        {
            and.format = and2;
            ios.format = ios2;
        }
        importer.SetPlatformTextureSettings(and);
        importer.SetPlatformTextureSettings(ios);
        importer.SaveAndReimport();
        #endregion

        #region 设置纹理方式二
        //if (importer.DoesSourceTextureHaveAlpha())
        //{
        //    importer.SetPlatformTextureSettings("iPhone", SizeType, TextureImporterFormat.ASTC_6x6);
        //    importer.SetPlatformTextureSettings("Android", SizeType, TextureImporterFormat.ETC2_RGBA8);
        //}
        //else
        //{
        //    importer.SetPlatformTextureSettings("iPhone", SizeType, TextureImporterFormat.ASTC_6x6);
        //    importer.SetPlatformTextureSettings("Android", SizeType, TextureImporterFormat.ETC2_RGB4);
        //}
        //importer.SaveAndReimport();
        #endregion

    }

    static int[] formatSize = new int[] { 32, 64, 128, 256, 512, 1024, 2048, 4096 };

    static int FitSize(int picValue)
    {
        foreach (var one in formatSize)
        {
            if (picValue <= one)
            {
                return one;
            }
        }

        return 1024;
    }

    private static string Replace(string str)
    {
        string s = str.Replace("\\", @"/");
        return s;
    }
}

参考:unity批量设置图片为etc2格式或者astc格式_渡水寻彼岸的博客-CSDN博客

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小张不爱写代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值