[unity]批量处理图片资源

原理比较简单, 原理比较简单textureImporter,直接上代码 效果为

[unity]批量处理图片资源_第1张图片

点确定就可以

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;
using System.IO;
using UnityEngine.EventSystems;


public class TextureImportPlatFormInfo
{

    public string Name;
    
    public TextureImporterFormat TextureFormat;

    public TextureImporterFormat HasAlphaChannelFormat;

    public int MaxSize;

    public bool isSetToggled;


}



public class TextureImportSetter : EditorWindow
{

    private string modelRootDir = Application.dataPath;
    public static TextureImporterFormat format = TextureImporterFormat.ETC2_RGB4;



    private TextureImportPlatFormInfo androidFormatInfo = new TextureImportPlatFormInfo()
    {
        MaxSize = 512,
        Name = "Android",
        TextureFormat = TextureImporterFormat.ETC2_RGB4,
        HasAlphaChannelFormat = TextureImporterFormat.ETC2_RGBA8
    };

    /// 
    /// ios导入格式
    /// 
    private TextureImportPlatFormInfo IosFormatInfo =
        new TextureImportPlatFormInfo()
        {
            MaxSize = 512,
            Name = "iPhone",
            TextureFormat = TextureImporterFormat.DXT5,
            HasAlphaChannelFormat = TextureImporterFormat.DXT5
        };


    private string searchFilters = "*.tga";

    private int[] formatSize = new int[]{32,64,128,256,512,1024,2048,4096};
    private string[] formatDes = new[] { "32", "64",  "128", "256", "512", "1024", "2048", "4096" };
    /// 
    /// 当前选中平台编号
    /// 
    private int curSelectPlatformIndex = 0 ;


    private bool isSelectAndroid
    {
        get { return curSelectPlatformIndex == 0; }
    }

    private bool isSelectIos
    {
        get { return curSelectPlatformIndex == 1; }
    }


    [MenuItem("Custom/SetTextureFormat")]
    public static void Init()
    {
        EditorWindow.GetWindow();
    }

    public void OnGUI()
    {

        EditorGUILayout.BeginVertical();
        if (GUILayout.Button("选择资源文件夹"))
        {
            modelRootDir = EditorUtility.OpenFolderPanel("模型根目录", modelRootDir, modelRootDir);
        }

        EditorGUILayout.LabelField("资源文件夹 : " + modelRootDir);
        GUILayout.BeginHorizontal();

        if (GUILayout.Toggle(isSelectAndroid, androidFormatInfo.Name ))
        {
            curSelectPlatformIndex = 0;
        }
        if (GUILayout.Toggle(isSelectIos, IosFormatInfo.Name))
        {
            curSelectPlatformIndex = 1;
        }

        GUILayout.EndHorizontal();

        if (isSelectAndroid)
        {
            DrawItemUI(androidFormatInfo);
        }

        if (isSelectIos)
        {
            DrawItemUI(IosFormatInfo);
        }

        if(GUILayout.Button("确定"))
        {
            SetFormat();
        }
        EditorGUILayout.EndVertical();
    }


    private void DrawItemUI(TextureImportPlatFormInfo platFormInfo)
    {
        platFormInfo.MaxSize = EditorGUILayout.IntPopup("图片大小", platFormInfo.MaxSize, formatDes, formatSize);

        platFormInfo.TextureFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("选择格式(不含alpht通道的格式)", platFormInfo.TextureFormat);
        platFormInfo.HasAlphaChannelFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("选择格式(含有alpht通道的格式)", platFormInfo.HasAlphaChannelFormat);

        platFormInfo.isSetToggled = EditorGUILayout.Toggle("选中该平台", platFormInfo.isSetToggled);
    }

    private void SetFormat()
    {
        if (!androidFormatInfo.isSetToggled && !IosFormatInfo.isSetToggled)
        {

            Debug.LogError("无设置打包平台");
            return;
        }

        var paths = Directory.GetFiles(modelRootDir,"*.*", SearchOption.AllDirectories).Where( s =>s.EndsWith(".png")|| s.EndsWith(".tga")|| s.EndsWith(".jpg") ) .ToArray();
        float progress = 0;

        for (int i = 0;i



你可能感兴趣的:([unity]批量处理图片资源)