[小工具记录]批量修改png图片格式

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

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,
    }

    // ----------------------------------------------------------------------------  
    TextureImporterType textureType = TextureImporterType.Sprite;
    TextureImporterFormat textureFormat = TextureImporterFormat.Automatic;
    MaxSize textureSize = MaxSize.Size_1024;
    TextureImporterCompression textureCompression = TextureImporterCompression.Uncompressed;

    bool ifAllowsAlphaSplitting = true;
    bool ifMipmapEnabled = false;

    string textureTag = "默认为父文件夹名";
    //平台
    bool isAndroid = true;
    bool isIOS = true;

    static TextureImportChanging window;
    [@MenuItem("Tools/Texture Settings")]
    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();

        textureType = (TextureImporterType)EditorGUILayout.EnumPopup("Type:", textureType);
        textureTag = EditorGUILayout.TextField("PackingTag", textureTag);
        textureFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("Fromat:", textureFormat);
        textureSize = (MaxSize)EditorGUILayout.EnumPopup("Size:", textureSize);
        textureCompression = (TextureImporterCompression)EditorGUILayout.EnumPopup("Compression:", textureCompression);
        ifAllowsAlphaSplitting = EditorGUILayout.Toggle("AlphaSplitting:", ifAllowsAlphaSplitting);
        ifMipmapEnabled = EditorGUILayout.Toggle("MipmapEnabled:", ifMipmapEnabled);
        EditorGUILayout.LabelField("设置平台 : ");
        GUILayout.BeginHorizontal();
        isAndroid = GUILayout.Toggle(isAndroid, "Android");
        isIOS = GUILayout.Toggle(isIOS, "IOS");
        GUILayout.EndHorizontal();


        EditorGUILayout.Space();

        if (GUILayout.Button("设置"))
        {
            TextureImporterPlatformSettings t = new TextureImporterPlatformSettings() {
                allowsAlphaSplitting = ifAllowsAlphaSplitting,
                format = textureFormat,
                maxTextureSize = (int)textureSize,
                textureCompression = textureCompression
            };


            SelectedChangeTextureFormatSettings(t, textureType, textureTag, isAndroid, isIOS);
        }

    }

    static void SelectedChangeTextureFormatSettings(TextureImporterPlatformSettings _t, TextureImporterType _type, string _tag, bool _isAndroid, bool _isIOS)
    {

        Object[] textures = GetSelectedTextures();
        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);
            //Debug.Log("path: " + path);
            string[] pathArr = path.Split('/');

            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            textureImporter.textureType = _type;

            textureImporter.wrapMode = TextureWrapMode.Clamp;
            textureImporter.filterMode = FilterMode.Bilinear;
            
            if (_tag == "默认为父文件夹名" || _tag == "")
                textureImporter.spritePackingTag = pathArr[pathArr.Length - 2];
            else
                textureImporter.spritePackingTag = _tag;
            

            textureImporter.SetPlatformTextureSettings(_t);
            if (_isAndroid)
            {
                _t.name = "Android";
                _t.overridden = true;
                textureImporter.SetPlatformTextureSettings(_t);
            }
            else
            {
                _t.name = "Android";
                _t.overridden = false;
                textureImporter.SetPlatformTextureSettings(_t);
            }
            if (_isIOS)
            {
                _t.name = "iPhone";
                _t.overridden = true;
                textureImporter.SetPlatformTextureSettings(_t);
            }
            else
            {
                _t.name = "iPhone";
                _t.overridden = false;
                textureImporter.SetPlatformTextureSettings(_t);
            }

            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);
    }
    void OnInspectorUpdate()
    {
        Repaint();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值