当前位置: 首页 > news >正文

Unity 一键修改图片缩放保存为当前的一半大小

用来压缩贴图大小还是比较方便的 支持 png,jpg,tga
话不多说 直接上代码

[MenuItem("Assets/扩展功能/缩放贴图一半尺寸(png | jpg | tga)", false)]static void ScaleHalfTextureSizeMenu(){foreach(var obj in Selection.objects) {Texture texObj = obj as Texture;if(texObj != null) {ScaleHalfTextureSize(texObj);}}}static void ScaleHalfTextureSize(Texture texAsset){if(texAsset == null) {return;}string texAssetPath = AssetDatabase.GetAssetPath(texAsset);string texPathExten = System.IO.Path.GetExtension(texAssetPath).ToLower();bool isTexJPG = texPathExten == ".jpg";bool isTexPNG = texPathExten == ".png";bool isTexTga = texPathExten == ".tga";if (!isTexJPG && !isTexPNG && !isTexTga) {Debug.LogError("不支持的图片格式-->" + texPathExten);return;}int width = texAsset.width;int height = texAsset.height;TextureFormat textureFormat = TextureFormat.RGBA32;Texture2D unCompressOriginTex;if (isTexTga){unCompressOriginTex = AssetDatabase.LoadAssetAtPath<Texture2D>(texAssetPath);}else{texAssetPath = Application.dataPath + texAssetPath.Remove(0, "Assets".Length);byte[] originByteDataArray = System.IO.File.ReadAllBytes(texAssetPath);unCompressOriginTex = new Texture2D(width, height, textureFormat, false);unCompressOriginTex.LoadImage(originByteDataArray, false);unCompressOriginTex.filterMode = FilterMode.Bilinear;unCompressOriginTex.wrapMode = TextureWrapMode.Clamp;unCompressOriginTex.Apply(false, true);}int halfWidth = width / 2;int halfHeight = height / 2;RenderTexture tempRT = RenderTexture.GetTemporary(halfWidth, halfHeight, 0, RenderTextureFormat.ARGB32);Graphics.Blit(unCompressOriginTex, tempRT);Texture2D halfSizeTex = new Texture2D(halfWidth, halfHeight, textureFormat, false);RenderTexture.active = tempRT;halfSizeTex.ReadPixels(new Rect(0, 0, halfWidth, halfHeight), 0, 0);if (isTexJPG) {System.IO.File.WriteAllBytes(texAssetPath, halfSizeTex.EncodeToJPG());}else if (isTexPNG) {System.IO.File.WriteAllBytes(texAssetPath, halfSizeTex.EncodeToPNG());}else if (isTexTga){System.IO.File.WriteAllBytes(texAssetPath, halfSizeTex.EncodeToTGA());}AssetDatabase.Refresh();RenderTexture.ReleaseTemporary(tempRT);if (isTexTga){//不能销毁}else{Object.DestroyImmediate(unCompressOriginTex);}    Object.DestroyImmediate(halfSizeTex);}

效果
在这里插入图片描述

http://www.lryc.cn/news/431104.html

相关文章:

  • Identifying User Goals from UI Trajectories论文学习
  • [STM32]从零开始的STM32标准库环境搭建(小白向)
  • 解决 Android 上的 .NET MAUI/Xamarin.AndroidX 应用调用 ASP.NET Core API 端点时 SSL 连接被拒绝的问题
  • 助贷CRM系统:为金融中介行业打造全新营销管理模式
  • HBase 部署及shell操作
  • 使用Fign进行客户端远程调用和SpringFormEncoder的使用
  • golang 数据库使用注意事项
  • Leetcode面试经典150题-221.最大正方形
  • 51单片机-DS1302,操作简述
  • Vue3+Vite+Echarts 出现Missing semicolon错误
  • iOS——frame和bounds的区别
  • Trm理论 3(注意力机制)
  • Vue2和Vue3项目创建的区别和 element ui 和element plus的导入方式
  • 基于STM32的猫狗宠物喂养系统设计(微信小程序)(215)
  • spark读取csv文件
  • 钢铁百科:Q420DR力学性能、Q420DR执行标准、Q420DR低温容器钢板
  • 三菱机器人手柄维修示教器维修手操器面板等
  • 中间件的学习理解总结
  • 编程秘密武器:提升工作效率的关键工具
  • Git+word记笔记
  • java-antrl手敲命令的hello world
  • 法规探讨 | 《医疗器械管理法(草案征求意见稿)》初探(1)
  • 大语言模型的上下文窗口(Context Windows):对人工智能应用的影响
  • Java【数组】
  • xAI巨无霸超级计算机上线:10万张H100 GPU,计划翻倍至20万张
  • python集合
  • 算法打卡 Day29(回溯算法)-复原 IP 地址 + 子集 + 子集 Ⅱ
  • LeetCode 热题100-17 缺失的第一个正数
  • 基于CloudflareSpeedTest项目实现git clone加速
  • 对与单纯post方法写项目的修改成baseservlet方法