zoukankan      html  css  js  c++  java
  • Unity一键设置导入图片格式

    前几天由于项目原因,做了一个自动根据模型自动创建动画状态机,然后紧接着做了根据动画状态机和模型一键制作Prefab. 现在因为图片数量或者其它原因需要写一个一键设置图片格式的插件.

    至于制作动画状态机和制作预制物体我会在以后的日子给大家贴出我的代码,下面开始进入今天的主题.

    需求: 点击文件夹,一键把文件里面的图片全部设置成需要的格式.

    思路: 第一步取到文件夹一直下的所有文件,第二步 过滤不是图片的文件, 第三部设置图片格式

    思路很简单下面先看一下取出所有文件的代码,思路是利用c#的文件递归的思想进行遍历文件,得到所有文件

        public void CollectModel(Object fold)
        {
            allControlls.Clear();
            string relatepath = AssetDatabase.GetAssetPath(fold);
            string folderpath = Path.GetFullPath(relatepath);
            CollectAllFiles(folderpath.Replace("\", "/").Replace(Application.dataPath, "Assets"));
        }
    
        public void CollectSkinOrAnimator(Object fold)
        {
            allControlls.Clear();
            string relatepath = AssetDatabase.GetAssetPath(fold);
            string folderpath = Path.GetFullPath(relatepath);
            CollectAnimatorFiles(folderpath.Replace("\", "/").Replace(Application.dataPath, "Assets"));
        }
        void CollectAnimatorFiles(string path)
        {
            string[] localfiles = Directory.GetFiles(path);
            string[] dirs = Directory.GetDirectories(path);
            for (int i = 0; i < localfiles.Length; i++)
            {
                string filepath = localfiles[i];
                if (filepath.Contains(".meta")) continue;
                Object obj = AssetDatabase.LoadAssetAtPath<Object>(filepath);
                if (!allControlls.Contains(obj))
                    allControlls.Add(obj);
            }
            for (int j = 0; j < dirs.Length; j++)
            {
                CollectAnimatorFiles(dirs[j]);
            }
        }
    
        void CollectAllFiles(string path)
        {
            string[] localfiles = Directory.GetFiles(path);
            string[] dirs = Directory.GetDirectories(path);
            for (int i = 0; i < localfiles.Length; i++)
            {
                string filepath = localfiles[i];
                if (filepath.Contains(".meta")) continue;
                if (!IsPicture(filepath)) continue;
                Object obj = AssetDatabase.LoadAssetAtPath<Object>(filepath);
                if (!allControlls.Contains(obj))
                    allControlls.Add(obj);
            }
            for (int j = 0; j < dirs.Length; j++)
            {
                CollectAllFiles(dirs[j]);
            }
        }
    View Code

    fold 是选中的文件夹,传入进来就会吧所有的文件读取到allControlls中去

    接下来看一下过滤图片的方法,上面我已经过滤了.meta文件和文件夹,接下来只需要判断是否是图片即可

    还是看代码比较实在

    private bool IsPicture(string filePath)
        {
            try
            {
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                BinaryReader reader = new BinaryReader(fs);
                string fileClass;
                byte buffer;
                buffer = reader.ReadByte();
                fileClass = buffer.ToString();
                buffer = reader.ReadByte();
                fileClass += buffer.ToString();
                reader.Close();
                fs.Close();
                if (fileClass == "255216" || fileClass == "7173" || fileClass == "13780" || fileClass == "6677")
                //255216是jpg;7173是gif;6677是BMP,13780是PNG
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
    View Code


    代码很简单就是读取文件,如果读取异常则不是图片,如果读取成功则判断文件的头文件是不是图片即可

    接下来到了重点,设置图片格式,代码也不是很难

    主要是对图片的用途设置,可读可写,是否使用mipmap和压缩格式等

      static void onPreprocessTexture(string path, TextureImportData textureImportData)
        {
            //自动设置类型;  
            TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
            textureImporter.textureType = textureImportData.textureImporterType;
            textureImporter.textureShape = textureImportData.ShapeType;
            textureImporter.isReadable = textureImportData.isReadable;
            textureImporter.mipmapEnabled = textureImportData.mipmapEnabled;
    
            TextureImporterPlatformSettings settingAndroid = textureImporter.GetPlatformTextureSettings("UNITY_ANDROID");
            settingAndroid.overridden = true;
            settingAndroid.format = textureImportData.textureImporterFormat;  //设置格式
            textureImporter.SetPlatformTextureSettings(settingAndroid);
    
            textureImporter.SaveAndReimport();
        }
    View Code

    发现少了一个关键的模型类,主要用于传数据使用

      public class TextureImportData
        {
            public TextureImporterType textureImporterType = TextureImporterType.Default;
            public bool isReadable = false;
            public bool mipmapEnabled = false;
            public TextureImporterShape ShapeType = TextureImporterShape.Texture2D;
            /// <summary>
            /// 图片压缩格式
            /// androdi TextureImporterFormat.ETC2_RGB4;
            /// ios TextureImporterFormat.PVRTC_RGB4
            /// </summary>
            public TextureImporterFormat textureImporterFormat = TextureImporterFormat.ETC2_RGB4;
    
        }
    View Code

    本人才疏学浅,如有不对欢迎大家指正

  • 相关阅读:
    Fragment中获取Activity的Context (转)
    raw cannot be resolved or is not a field解决办法
    商业分析07_06分布情况分析
    商业分析07_04漏斗分析
    商业分析07_03数据涨跌异动如何处理
    商业分析07_02多维度拆解
    商业分析07_01对比分析
    商业分析07_00概述 数据分析
    商业分析06选择数据工具
    商业分析05如何选取数据指标
  • 原文地址:https://www.cnblogs.com/mawanli/p/8530248.html
Copyright © 2011-2022 走看看