找回密码
 立即注册
查看: 367|回复: 0

(Unity) 批量修改图片压缩工具

[复制链接]
发表于 2021-12-21 18:10 | 显示全部楼层 |阅读模式
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class ChangeTexSettingWindow :EditorWindow
{

    enum TargetPlatform
    {
        Standalone,
        IOS,
        Android
    }
    [MenuItem("Tools/ChangeTexSettingsWindow")]
    public static void OpenChangeTexSettingsWindow()
    {
        ChangeTexSettingWindow window = EditorWindow.CreateInstance<ChangeTexSettingWindow>();
        window.Show();
    }

    string TexPath;
    string TexSuffix = "*.bmp|*.jpg|*.gif|*.png|*.tif|*.psd";
    TargetPlatform SelectPlatform = TargetPlatform.Android;
    TextureImporterFormat WithAlpha = TextureImporterFormat.ASTC_4x4;
    TextureImporterFormat WithoutAlpha = TextureImporterFormat.PVRTC_RGB4;
    private void OnGUI()
    {
        GUILayout.BeginHorizontal();
        TexPath = EditorGUILayout.TextField("图片资源路径", TexPath);
        if (GUILayout.Button(EditorGUIUtility.IconContent("Folder Icon"), GUILayout.Width(18), GUILayout.Height(18)))
        {
            TexPath = EditorUtility.OpenFolderPanel("图片路径选择", "", "");
        }
        GUILayout.EndHorizontal();
        GUI.enabled = false;
        GUILayout.BeginHorizontal();
        TexSuffix = EditorGUILayout.TextField("图片格式", TexSuffix);
        GUILayout.EndHorizontal();
        GUI.enabled = true;
        GUILayout.BeginHorizontal();
        SelectPlatform = (TargetPlatform)Enum.Parse(typeof(TargetPlatform), EditorGUILayout.EnumPopup("选择目标平台", SelectPlatform).ToString());
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        WithAlpha = (TextureImporterFormat)Enum.Parse(typeof(TextureImporterFormat), EditorGUILayout.EnumPopup("有Alpha通道", WithAlpha).ToString());
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        WithoutAlpha = (TextureImporterFormat)Enum.Parse(typeof(TextureImporterFormat), EditorGUILayout.EnumPopup("没有Alpha通道", WithoutAlpha).ToString());
        GUILayout.EndHorizontal();
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("转换"))
        {
            if (string.IsNullOrEmpty(TexPath) || !Directory.Exists(TexPath))
            {
                EditorUtility.DisplayDialog("错误", "路径不能为空或路径不存在", "确定");
                return;
            }
            if (string.IsNullOrEmpty(TexSuffix))
            {
                EditorUtility.DisplayDialog("错误", "路径不能为空或路径不存在", "确定");
                return;
            }
            List<string> lst = GetAllTexPaths(TexPath);
            TextureImporterPlatformSettings settings = new TextureImporterPlatformSettings();
            settings.name = SelectPlatform.ToString();
            settings.crunchedCompression = true;
            settings.overridden = true;
            int i = 0;
            EditorUtility.DisplayProgressBar("修改", "修改图片格式", 0);
            for (; i < lst.Count; i++)
            {
                Change(lst, settings);
                EditorUtility.DisplayProgressBar("转换", string.Format("修改图片格式    {0}/{1}", i, lst.Count), i / (float)lst.Count);
            }
            AssetDatabase.SaveAssets();
            EditorUtility.ClearProgressBar();
        }
        GUILayout.EndHorizontal();
    }
    private void Change(string path, TextureImporterPlatformSettings platformSettings)
    {
        path = path.Substring(path.IndexOf("Assets"));
        try
        {
            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            if (textureImporter.DoesSourceTextureHaveAlpha())
            {
                platformSettings.format = WithAlpha;
            }
            else
            {
                platformSettings.format = WithoutAlpha;
            }
            textureImporter.SetPlatformTextureSettings(platformSettings);
            textureImporter.SaveAndReimport();
            AssetDatabase.ImportAsset(path);
        }
        catch
        {
            AssetDatabase.SaveAssets();
            EditorUtility.ClearProgressBar();
        }
    }


    private List<string> GetAllTexPaths(string rootPath)
    {
        List<string> lst = new List<string>();
        string[] types = TexSuffix.Split('|');
        for (int i = 0; i < types.Length; i++)
        {
            lst.AddRange(Directory.GetFiles(rootPath, types, SearchOption.AllDirectories));
        }
        return lst;
    }
}那个图片格式不知道怎么正则匹配,所以先这么写了
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-23 00:33 , Processed in 0.089095 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表