unity 图片格式一键修改

  1. unity 可以通过修改图片格式来减少包体大小,不同平台对不用格式图片的加载速度,也有差别.在开发WebGl是因为没有开发好的工具,要对图片进行一个处理,自己写了一个脚本工具类,来进行图片适配WebGl端.
  2. 最后的效果,用过选择文件夹来处理文件夹下的图片,不用人工一张一张设置了.

代码:

先创建一个TextureImportChanging类,继承EditorWindow类.

主要方法:先设置一个设置图片格式的界面,

using UnityEditor;
using UnityEngine;

public class TextureImportChanging : EditorWindow
{
    enum MaxSize
    {
        Size_32 = 32,
        Size_64 = 64,
        Size_128 = 128,
        Size_256 = 256,
        Size_512 = 512,
        Size_1024 = 1024,
        Size_2048 = 2048,
        Size_4096 = 4096,
        Size_8192 = 8192,
    }

    private static TextureImporterFormat textureImporterFormat
    {
        get
        {
#if UNITY_WEBGL
            return TextureImporterFormat.DXT5Crunched;
#else
            return TextureImporterFormat.Automatic;
#endif
        }
    }
    // ----------------------------------------------------------------------------  
    static TextureImporterType textureType = TextureImporterType.Sprite;
    /// <summary>
    /// 格式
    /// </summary>
    static TextureImporterFormat textureFormat = textureImporterFormat;
    MaxSize textureSize = MaxSize.Size_1024;
    /// <summary>
    /// 压缩
    /// </summary>
    TextureImporterCompression textureCompression = TextureImporterCompression.Uncompressed;
    static TextureWrapMode wrapMode = TextureWrapMode.Repeat;

    static FilterMode filterMode = FilterMode.Bilinear;
    bool ifAllowsAlphaSplitting = true;
    /// <summary>
    /// 是否允许Mipmap
    /// </summary>
    static bool ifMipmapEnabled = false;

    /// <summary>
    /// 是否重置图片属性
    /// </summary>
    static bool textureModifier = true;
    /// <summary>
    /// 是否改变图片类型
    /// </summary>
    bool isChangeTextureType = false;
    /// <summary>
    /// 是否固定的改变图片格式
    /// </summary>
    static bool isChangeTexturFormat = false;
    /// <summary>
    /// 是否统一图片最压缩大尺寸,true 所选的图片的override的maxSize都会设置为所选尺寸,false都设置为图片原生尺寸的宽高取最大值
    /// </summary>
    static bool isUnificationSize = false;

    static TextureImportChanging window;
    [@MenuItem("资源管理/设置图片格式")]
    private static void Init()
    {
        Rect wr = new Rect(0 , 0 , 400 , 400);
        window = (TextureImportChanging)EditorWindow.GetWindowWithRect(typeof(TextureImportChanging) , wr , false , "图片格式设置");
        window.Show();
    }

    private void OnGUI()
    {
        EditorGUILayout.Space();
        EditorGUILayout.HelpBox("设置选中图片或选中路径下的图片属性" , MessageType.Info);
        EditorGUILayout.Space();

        isChangeTextureType = EditorGUILayout.Toggle("是否改变图片类型:" , isChangeTextureType);
        if (isChangeTextureType)
            textureType = (TextureImporterType)EditorGUILayout.EnumPopup("类型:" , textureType);
        EditorGUILayout.Space();

        isChangeTexturFormat = EditorGUILayout.Toggle("是否固定改变图片格式:" , isChangeTexturFormat);
        if (isChangeTexturFormat)
            textureFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("格式:" , textureFormat);
        EditorGUILayout.Space();

        textureCompression = (TextureImporterCompression)EditorGUILayout.EnumPopup("压缩:" , textureCompression);

        ifAllowsAlphaSplitting = EditorGUILayout.Toggle("是否允许透明分离:" , ifAllowsAlphaSplitting);
        ifMipmapEnabled = EditorGUILayout.Toggle("是否允许Mipmap:" , ifMipmapEnabled);
        textureModifier = EditorGUILayout.Toggle("是否重置图片属性:" , textureModifier);

        if (!textureModifier)
        {
            EditorGUILayout.Space();
            EditorGUILayout.HelpBox("如果有导入自动设置图片脚本,下面方法未必执行\n 统一OverrideSize值,则所选图片的该值都设置为所选大小,否则为图片的宽高最大值" , MessageType.Info);
            EditorGUILayout.Space();
            isUnificationSize = EditorGUILayout.Toggle("是否统一Override MaxSize:" , isUnificationSize);
            if (isUnificationSize)
                textureSize = (MaxSize)EditorGUILayout.EnumPopup("尺寸:" , textureSize);
            wrapMode = (TextureWrapMode)EditorGUILayout.EnumPopup("wrapMode:" , wrapMode);
            filterMode = (FilterMode)EditorGUILayout.EnumPopup("filterMode:" , filterMode);
        }

        EditorGUILayout.Space();

        if (GUILayout.Button("设置"))
        {
            TextureImporterPlatformSettings t = new TextureImporterPlatformSettings();

            t.allowsAlphaSplitting = ifAllowsAlphaSplitting;
            t.format = textureFormat;

            t.maxTextureSize = (int)textureSize;
            t.textureCompression = textureCompression;

            SelectedChangeTextureFormatSettings(t , textureType , GetSelectedTextures());
        }

        //if (GUILayout.Button("一键设置(Textures目录)")) {
        //    TextureImporterPlatformSettings t = new TextureImporterPlatformSettings();

        //    t.allowsAlphaSplitting = ifAllowsAlphaSplitting;
        //    t.format = textureFormat;

        //    t.maxTextureSize = (int)textureSize;
        //    t.textureCompression = textureCompression;

        //    SelectedChangeTextureFormatSettings(t, textureType,GetTexture());
        //}

    }

    void SelectedChangeTextureFormatSettings(TextureImporterPlatformSettings _t , TextureImporterType _type , Object[] arr)
    {

        Object[] textures = arr;
        if (window == null)
            Init();
        if (textures != null)
        {
            if (textures.Length < 1)
            {
                window.ShowNotification(new GUIContent("找不到图片!"));
                return;
            }
        }
        else
        {
            window.ShowNotification(new GUIContent("请选中图片或路径!"));
            return;
        }
        Selection.objects = new Object[0];
        int i = 0;
        foreach (Texture2D texture in textures)
        {
            string path = AssetDatabase.GetAssetPath(texture);

            if (path.Substring(path.Length - 3 , 3) == "dds")
                continue;
            string[] pathArr = path.Split('/');

            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            if (!isUnificationSize)
                _t.maxTextureSize = GetMaxSize(texture.height , texture.width);
            if (isChangeTextureType)
                textureImporter.textureType = _type;


            textureImporter.allowAlphaSplitting = _t.allowsAlphaSplitting;
            textureImporter.mipmapEnabled = ifMipmapEnabled;
            textureImporter.borderMipmap = ifMipmapEnabled;
            textureImporter.textureCompression = _t.textureCompression;
            if (!textureModifier)
            {
                textureImporter.maxTextureSize = _t.maxTextureSize;
                textureImporter.wrapMode = wrapMode;
                textureImporter.filterMode = filterMode;
                textureImporter.spritePackingTag = pathArr[pathArr.Length - 2];

                TextureImporterPlatformSettings webglPlatFormSetting = textureImporter.GetPlatformTextureSettings("WebGl");
                webglPlatFormSetting.overridden = true;
                webglPlatFormSetting.format = GetFormat(textureImporter , "WebGl");
                webglPlatFormSetting.maxTextureSize = _t.maxTextureSize;
                textureImporter.SetPlatformTextureSettings(webglPlatFormSetting);

                TextureImporterPlatformSettings pcPlatFormSetting = textureImporter.GetPlatformTextureSettings("PC");
                pcPlatFormSetting.overridden = true;
                pcPlatFormSetting.format = GetFormat(textureImporter , "PC");
                pcPlatFormSetting.maxTextureSize = _t.maxTextureSize;
                textureImporter.SetPlatformTextureSettings(pcPlatFormSetting);
            }

            ShowProgress((float)i / (float)textures.Length , textures.Length , i);
            i++;
            AssetDatabase.ImportAsset(path);
        }
        AssetDatabase.Refresh();
        EditorUtility.ClearProgressBar();
        textures = null;
    }
    public static void ShowProgress(float val , int total , int cur)
    {
        EditorUtility.DisplayProgressBar("设置图片中..." , string.Format("请稍等({0}/{1}) " , cur , total) , val);
    }


    static Object[] GetSelectedTextures()
    {
        return Selection.GetFiltered(typeof(Texture2D) , SelectionMode.DeepAssets);
    }

    static TextureImporterFormat GetFormat(TextureImporter item , string platformName)
    {
        if (isChangeTexturFormat)
            return textureFormat;
        TextureImporterFormat format = textureFormat;
        bool isNormalMap = item.textureType == TextureImporterType.NormalMap;
        switch (platformName)
        {
            case "WebGl":
                format = isNormalMap || item.DoesSourceTextureHaveAlpha() ? TextureImporterFormat.DXT5Crunched : TextureImporterFormat.DXT1Crunched;
                break;
            case "PC":
                format = isNormalMap || item.DoesSourceTextureHaveAlpha() ? TextureImporterFormat.DXT5 : TextureImporterFormat.DXT1;
                break;
            default:
                break;
        }
        return format;

    }

    static int GetMaxSize(int height , int width)
    {
        return height >= width ? height : width;
    }
    void OnInspectorUpdate()
    {
        Repaint();
    }

}

第一次写这种工具,也是在别人的基础上修改的,有大佬可以指出里面逻辑不对的地方

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值