zoukankan      html  css  js  c++  java
  • Unity之图片压缩,批量设置,导入自动设置

    代码如下:

    批量设置:

      1 using System.Collections.Generic;
      2 using System.IO;
      3 using UnityEditor;
      4 using UnityEngine;
      5 
      6 public class TextureCompressTool : ScriptableObject
      7 {
      8     [MenuItem("FashionBeat/Compress texture")]
      9     static void CompressSelectTexture()
     10     {
     11         List<string> list = new List<string>();
     12 
     13         foreach (var so in Selection.objects)
     14         {
     15             bool isFolder = so is DefaultAsset;
     16             string selectObjPath = AssetDatabase.GetAssetPath(so);
     17             if (isFolder)
     18             {
     19                 string[] files = Directory.GetFiles(selectObjPath, "*.*", SearchOption.AllDirectories);
     20                 for (int i = 0; i < files.Length; i++)
     21                 {
     22                     string filepath = files[i];
     23                     if (FBEditorHelper.IsImage(filepath))
     24                     {
     25                         string assetpath = FBEditorHelper.GetAssetPath(filepath);
     26                         if (!list.Contains(assetpath))
     27                             list.Add(assetpath);
     28                     }
     29                 }
     30             }
     31             else
     32             {
     33                 if (!list.Contains(selectObjPath))
     34                     list.Add(selectObjPath);
     35             }
     36         }
     37 
     38         for (int i = 0; i < list.Count; i++)
     39         {
     40             string assetpath = list[i];
     41             CompressTexture(assetpath);
     42             EditorUtility.DisplayProgressBar("批量处理图片", assetpath, (float)i / list.Count);
     43         }
     44 
     45         EditorUtility.ClearProgressBar();
     46         AssetDatabase.Refresh();
     47         Debug.Log("All done, count: " + list.Count);
     48     }
     49 
     50     static void CompressTexture(string assetpath)
     51     {
     52         // 根据路径获得文件目录,设置图集的packagingTag
     53         TextureImporter textureImporter = AssetImporter.GetAtPath(assetpath) as TextureImporter;
     54         CompressTexture(textureImporter);
     55     }
     56 
     57     public static void CompressTexture(TextureImporter textureImporter)
     58     {
     59         // 根据路径获得文件目录,设置图集的packagingTag
     60         if (!textureImporter)
     61         {
     62             Debug.LogWarning("textureImporter == null");
     63             return;
     64         }
     65         if (textureImporter.textureType != TextureImporterType.Default)
     66         {
     67             Debug.LogWarning("This texture is not Default Type: " + textureImporter.assetPath, textureImporter);
     68             return;
     69         }
     70         bool haveAlpha = textureImporter.DoesSourceTextureHaveAlpha();
     71 
     72         textureImporter.alphaIsTransparency = haveAlpha;
     73         textureImporter.mipmapEnabled = false;
     74         textureImporter.isReadable = false;
     75         textureImporter.textureType = TextureImporterType.Default;
     76         //textureImporter.wrapMode = TextureWrapMode.Clamp;
     77         textureImporter.npotScale = TextureImporterNPOTScale.ToNearest;
     78         textureImporter.textureCompression = TextureImporterCompression.Compressed;
     79 
     80         // Android 端单独设置
     81         TextureImporterPlatformSettings settingAndroid = new TextureImporterPlatformSettings()
     82         {
     83             name = "Android",
     84             overridden = true,
     85             format = haveAlpha ? TextureImporterFormat.ETC2_RGBA8 : TextureImporterFormat.ETC_RGB4,
     86         };
     87         textureImporter.SetPlatformTextureSettings(settingAndroid);
     88 
     89         // IOS端单独设置
     90         TextureImporterPlatformSettings settingIOS = new TextureImporterPlatformSettings()
     91         {
     92             name = "iOS",
     93             overridden = true,
     94             format = TextureImporterFormat.ASTC_6x6,
     95         };
     96         textureImporter.SetPlatformTextureSettings(settingIOS);
     97 
     98         textureImporter.SaveAndReimport();
     99 
    100         Debug.Log("Compress texture done: " + textureImporter.assetPath);
    101     }
    102 
    103 }

    导入自动设置:

     1 using UnityEditor;
     2 using UnityEngine;
     3 
     4 public class AssetImporterProcessor : AssetPostprocessor
     5 {
     6     void OnPostprocessTexture(Texture2D texture)
     7     {
     8         TextureCompressTool.CompressTexture(assetImporter as TextureImporter);
     9     }
    10 }

    转载请注明出处:https://www.cnblogs.com/jietian331/p/15433179.html

  • 相关阅读:
    ASP.NET Zero--10.一个例子(3)商品分类管理-新建
    ASP.NET Zero--9.一个例子(2)商品分类管理-列表
    ASP.NET Zero--8.一个例子(1)菜单添加
    ASP.NET Zero--7.控制器加权限
    ASP.NET Zero--6.菜单加权限
    ASP.NET Zero--5.配置权限
    ASP.NET Zero--4.不使用谷歌字体,提升加载速度
    ASP.NET Zero--2.如何启动
    C# mongoDB Driver 使用对象方式最完善查询语法大全
    ef 数据库连接字符串加密
  • 原文地址:https://www.cnblogs.com/jietian331/p/15433179.html
Copyright © 2011-2022 走看看