1. unity 可以通过修改图片格式来减少包体大小,不同平台对不用格式图片的加载速度,也有差别.在开发WebGl是因为没有开发好的工具,要对图片进行一个处理,自己写了一个脚本工具类,来进行图片适配WebGl端.
  2. 最后的效果,用过选择文件夹来处理文件夹下的图片,不用人工一张一张设置了.

代码:

先创建一个TextureImportChanging类,继承EditorWindow类.

主要方法:先设置一个设置图片格式的界面,

using UnityEditor;
using UnityEngine;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,}private static TextureImporterFormat textureImporterFormat{get{
#if UNITY_WEBGLreturn TextureImporterFormat.DXT5Crunched;
#elsereturn TextureImporterFormat.Automatic;
#endif}}// ----------------------------------------------------------------------------  static TextureImporterType textureType = TextureImporterType.Sprite;/// <summary>/// 格式/// </summary>static TextureImporterFormat textureFormat = textureImporterFormat;MaxSize textureSize = MaxSize.Size_1024;/// <summary>/// 压缩/// </summary>TextureImporterCompression textureCompression = TextureImporterCompression.Uncompressed;static TextureWrapMode wrapMode = TextureWrapMode.Repeat;static FilterMode filterMode = FilterMode.Bilinear;bool ifAllowsAlphaSplitting = true;/// <summary>/// 是否允许Mipmap/// </summary>static bool ifMipmapEnabled = false;/// <summary>/// 是否重置图片属性/// </summary>static bool textureModifier = true;/// <summary>/// 是否改变图片类型/// </summary>bool isChangeTextureType = false;/// <summary>/// 是否固定的改变图片格式/// </summary>static bool isChangeTexturFormat = false;/// <summary>/// 是否统一图片最压缩大尺寸,true 所选的图片的override的maxSize都会设置为所选尺寸,false都设置为图片原生尺寸的宽高取最大值/// </summary>static bool isUnificationSize = false;static TextureImportChanging window;[@MenuItem("资源管理/设置图片格式")]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();isChangeTextureType = EditorGUILayout.Toggle("是否改变图片类型:" , isChangeTextureType);if (isChangeTextureType)textureType = (TextureImporterType)EditorGUILayout.EnumPopup("类型:" , textureType);EditorGUILayout.Space();isChangeTexturFormat = EditorGUILayout.Toggle("是否固定改变图片格式:" , isChangeTexturFormat);if (isChangeTexturFormat)textureFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("格式:" , textureFormat);EditorGUILayout.Space();textureCompression = (TextureImporterCompression)EditorGUILayout.EnumPopup("压缩:" , textureCompression);ifAllowsAlphaSplitting = EditorGUILayout.Toggle("是否允许透明分离:" , ifAllowsAlphaSplitting);ifMipmapEnabled = EditorGUILayout.Toggle("是否允许Mipmap:" , ifMipmapEnabled);textureModifier = EditorGUILayout.Toggle("是否重置图片属性:" , textureModifier);if (!textureModifier){EditorGUILayout.Space();EditorGUILayout.HelpBox("如果有导入自动设置图片脚本,下面方法未必执行\n 统一OverrideSize值,则所选图片的该值都设置为所选大小,否则为图片的宽高最大值" , MessageType.Info);EditorGUILayout.Space();isUnificationSize = EditorGUILayout.Toggle("是否统一Override MaxSize:" , isUnificationSize);if (isUnificationSize)textureSize = (MaxSize)EditorGUILayout.EnumPopup("尺寸:" , textureSize);wrapMode = (TextureWrapMode)EditorGUILayout.EnumPopup("wrapMode:" , wrapMode);filterMode = (FilterMode)EditorGUILayout.EnumPopup("filterMode:" , filterMode);}EditorGUILayout.Space();if (GUILayout.Button("设置")){TextureImporterPlatformSettings t = new TextureImporterPlatformSettings();t.allowsAlphaSplitting = ifAllowsAlphaSplitting;t.format = textureFormat;t.maxTextureSize = (int)textureSize;t.textureCompression = textureCompression;SelectedChangeTextureFormatSettings(t , textureType , GetSelectedTextures());}//if (GUILayout.Button("一键设置(Textures目录)")) {//    TextureImporterPlatformSettings t = new TextureImporterPlatformSettings();//    t.allowsAlphaSplitting = ifAllowsAlphaSplitting;//    t.format = textureFormat;//    t.maxTextureSize = (int)textureSize;//    t.textureCompression = textureCompression;//    SelectedChangeTextureFormatSettings(t, textureType,GetTexture());//}}void SelectedChangeTextureFormatSettings(TextureImporterPlatformSettings _t , TextureImporterType _type , Object[] arr){Object[] textures = arr;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);if (path.Substring(path.Length - 3 , 3) == "dds")continue;string[] pathArr = path.Split('/');TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;if (!isUnificationSize)_t.maxTextureSize = GetMaxSize(texture.height , texture.width);if (isChangeTextureType)textureImporter.textureType = _type;textureImporter.allowAlphaSplitting = _t.allowsAlphaSplitting;textureImporter.mipmapEnabled = ifMipmapEnabled;textureImporter.borderMipmap = ifMipmapEnabled;textureImporter.textureCompression = _t.textureCompression;if (!textureModifier){textureImporter.maxTextureSize = _t.maxTextureSize;textureImporter.wrapMode = wrapMode;textureImporter.filterMode = filterMode;textureImporter.spritePackingTag = pathArr[pathArr.Length - 2];TextureImporterPlatformSettings webglPlatFormSetting = textureImporter.GetPlatformTextureSettings("WebGl");webglPlatFormSetting.overridden = true;webglPlatFormSetting.format = GetFormat(textureImporter , "WebGl");webglPlatFormSetting.maxTextureSize = _t.maxTextureSize;textureImporter.SetPlatformTextureSettings(webglPlatFormSetting);TextureImporterPlatformSettings pcPlatFormSetting = textureImporter.GetPlatformTextureSettings("PC");pcPlatFormSetting.overridden = true;pcPlatFormSetting.format = GetFormat(textureImporter , "PC");pcPlatFormSetting.maxTextureSize = _t.maxTextureSize;textureImporter.SetPlatformTextureSettings(pcPlatFormSetting);}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);}static TextureImporterFormat GetFormat(TextureImporter item , string platformName){if (isChangeTexturFormat)return textureFormat;TextureImporterFormat format = textureFormat;bool isNormalMap = item.textureType == TextureImporterType.NormalMap;switch (platformName){case "WebGl":format = isNormalMap || item.DoesSourceTextureHaveAlpha() ? TextureImporterFormat.DXT5Crunched : TextureImporterFormat.DXT1Crunched;break;case "PC":format = isNormalMap || item.DoesSourceTextureHaveAlpha() ? TextureImporterFormat.DXT5 : TextureImporterFormat.DXT1;break;default:break;}return format;}static int GetMaxSize(int height , int width){return height >= width ? height : width;}void OnInspectorUpdate(){Repaint();}}

第一次写这种工具,也是在别人的基础上修改的,有大佬可以指出里面逻辑不对的地方

unity 图片格式一键修改相关推荐

  1. mac电脑如何转换图片格式及修改大小?

    mac电脑如何转换图片格式及修改大小? !转换图片格式 !转换图片格式 !转换图片大小 以上,希望能对大家有所帮助!

  2. 详细步骤:SCI等论文投稿,修改图片格式visio-eps,无需ps

    读研一年半了,投过了好多期刊,修改图片格式着实令人头疼,最近痛定思痛,打算一定搞懂.今天就来介绍一下不使用ps情况下,图片格式的修改. 投稿分为word和latex版的,一般word用png或者pdf ...

  3. CAD图纸转换TIFF格式时修改背景颜色

    接触CAD相关工作的小伙伴,常常会对CAD图纸进行格式转换,CAD图纸转换图片格式就是较为常见的,这其中就有CAD图纸转换成TIFF图片格式.而CAD图纸转换TIFF图片格式的时候,可能需要修改背景颜 ...

  4. 一键修改文件夹下图片文件的格式,适用于任何格式

    我的文件分布如上,本方法可实现一键修改所有图片文件的格式,例如将现有的png格式修改为jpg格式. 下面上代码: import os from PIL import Image # 图片路径 CONV ...

  5. PhotoShop一键修改4的倍数图片工具

    PhotoShop一键修改4的倍数图片工具 1.工具简介 2.安装 3.使用 3-1.是否拉伸画布 3-2.设置重新采样的方式(拉伸画布时无效) 3-3.是否使用图片本来的分辨率 3-4.选择输出方式 ...

  6. 【mmdetection小目标检测教程】三、使用sahi库切分高分辨率图片,一键生成coco格式数据集

    [mmdetection小目标检测教程]三.使用sahi库切分高分辨率图片,一键生成coco格式数据集 1.确认是否需要切分图像 2.子图切分 (1)安装sahi库 (2)基于sahi切图 本文我们将 ...

  7. 电脑图片如何转换成jpg?怎样一键改图片格式?

    在日常工作中需要将其他图片格式转换为jpg,如何一键修改图片格式?使用压缩图在线图片转格式工具,上传图片即可快速转换格式,以png转jpg为例,具体操作如下. 1.上传图片. 2.点击选择jpg格式, ...

  8. 图片格式怎么转换?JPG、PNG、BMP等格式一键转换,这三个工具收好

    推荐3个格式丰富的图片转换工具,转换后的图片依旧跟原图一样清晰,而且还支持批量设置转换图片,其中一个还支持免费批量转换图片格式,而且转换后的图片还无需下载. 1.在线图片转换网站 一个免费的在线图片格 ...

  9. Origin一键复制粘贴,也能批量更改图片格式

      Origin作图无疑是比较全能的,做出来的图形好看,而且图片质量高.但有一个问题,就是图片格式的调整,尤其是相同图片格式的调整,经验不足的同学可能会一张一张的进行调节,比较麻烦.   之前给大家讲 ...

最新文章

  1. .net wap强制输出WML
  2. 面试官:Java 到底是值传递还是引用传递?
  3. [Scoi2016]背单词[字典树+dfs重构树[类似虚树]]
  4. 【Windows系统】基于vscode搭建go语言开发环境
  5. Jupyter notebook与Spyder集成
  6. android实现底部弹出菜单,Android实现底部缓慢弹出菜单
  7. 转】用Maven构建Mahout项目
  8. 计数信号量的原理与创建
  9. mysql 换行_教你如何用Python 连接 MySQL
  10. 北京80后整体亮相《北京作家》
  11. C++类对象作为类成员
  12. 19-A Walk-based Model on Entity Graphs for Relation Extraction(句内多对,多关系,多元,2018ACL
  13. 痛失 3000 万美元比特币现金,只因一张 SIM 卡?
  14. 第二十四章 异常和错误处理 6异常类与模板的关系
  15. 网站视频很卡怎么办?有没有免费的视频平台?使用阿里云OSS对象云存储+下行流量包解决网站文件/视频访问慢问题
  16. 阿里20亿美金收购考拉,丁磊到底是怎么“失身”的?
  17. 洛谷P2123 皇后游戏
  18. 柏林是哪个系统的服务器,柏林系统其实很“佛系”,明白了这些你也能玩好海缸!...
  19. 编程语言之时下潮流与实用价值
  20. 协同数据交换平台详细设计方案(word)

热门文章

  1. 拒绝断网失联!卫星电话成为户外徒步、海洋通信等领域的必备终端
  2. php日历类 农历,PHP完整的日历类(CLASS)
  3. 手机如何投屏到电视?苹果手机的3种投屏方法,5分钟全部搞定!
  4. 计算机系统 专辑 视频
  5. 百年历史一脉相承——Barsetto百胜图咖啡
  6. GeoTools:WKT、GeoJson、Feature、FeatureCollection相互转换常用工具
  7. 我们追求的泛化,竟是一条死路?
  8. 彻底禁止win10更新
  9. 占位隐藏 html,CSS设置输入框占位符placeholder点击隐藏
  10. 四级网络工程师笔记-操作系统(上)