美文网首页
Unity检查当前文件夹图片压缩格式(ASTC,ETC等)

Unity检查当前文件夹图片压缩格式(ASTC,ETC等)

作者: 小骄傲999 | 来源:发表于2021-12-07 12:38 被阅读0次
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using UnityEditor;
    using UnityEngine;
    
    public class TextureAutoSet : EditorWindow
    {
        
        [MenuItem("Assets/检查图片压缩格式", priority = 0)]
        static void AutoSetASTC()
        {
            string[] guidArray = Selection.assetGUIDs;
            Debug.Log(guidArray.Length);
            foreach (var item in guidArray)
            {
                string selectFloder = AssetDatabase.GUIDToAssetPath(item);
                DirectoryInfo root = new DirectoryInfo(selectFloder);
                GetFloder(root);
            }
        }
    
        static void GetFloder(DirectoryInfo root)
        {
            GetFile(root);
            //查找子文件夹
            DirectoryInfo[] array = root.GetDirectories();
            foreach (DirectoryInfo item in array)
            {
                GetFloder(item);
            }
        }
    
        static void GetFile(DirectoryInfo root)
        {
            //DirectoryInfo root = new DirectoryInfo(path);
            FileInfo[] fileDic = root.GetFiles();
            foreach (var file in fileDic)
            {
                //sDebug.Log(file);
                if (file.FullName.EndsWith(".png") || file.FullName.EndsWith(".jpg") || file.FullName.EndsWith(".tga") ||
                    file.FullName.EndsWith(".psd") || file.FullName.EndsWith(".PSD") || file.FullName.EndsWith(".exr") ||
                    file.FullName.EndsWith(".tif"))
                {
                    //Debug.Log("-------------" + file.FullName);
                    //Debug.Log(Application.dataPath);
                    SetPicFormat(file.FullName.Replace('\\','/').Replace(Application.dataPath.Replace("Assets", ""), ""));
                }
            }
        }
    
        static void SetPicFormat(string path)
        {
            TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
            if (importer==null)
            {
                return;
            }
            var tempsss = importer.GetPlatformTextureSettings("Android");
            bool isNeedFormat = false;
            switch (tempsss.format)
            {
                case TextureImporterFormat.ASTC_RGB_4x4:
                case TextureImporterFormat.ASTC_RGB_5x5:
                case TextureImporterFormat.ASTC_RGB_6x6:
                case TextureImporterFormat.ASTC_RGB_8x8:
                case TextureImporterFormat.ASTC_RGB_10x10:
                case TextureImporterFormat.ASTC_RGB_12x12:
                case TextureImporterFormat.ASTC_RGBA_4x4:
                case TextureImporterFormat.ASTC_RGBA_5x5:
                case TextureImporterFormat.ASTC_RGBA_6x6:
                case TextureImporterFormat.ASTC_RGBA_8x8:
                case TextureImporterFormat.ASTC_RGBA_10x10:
                case TextureImporterFormat.ASTC_RGBA_12x12:
                    Debug.Log(path);
                    isNeedFormat = true;
                    break;
                default:
                    break;
            }
            if (!isNeedFormat || !tempsss.overridden)
            {
                Debug.Log("设置图片格式"+path);
                tempsss.overridden = true;
                tempsss.format = TextureImporterFormat.ASTC_RGBA_6x6;
                tempsss.maxTextureSize = 1024;
                importer.SetPlatformTextureSettings(tempsss);
                importer.SaveAndReimport();
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Unity检查当前文件夹图片压缩格式(ASTC,ETC等)

          本文链接:https://www.haomeiwen.com/subject/zyooxrtx.html