[Unity]将所有 TGA、TIFF、PSD 和 BMP(可自定义)纹理转换为 PNG,以减小项目大小,而不会在 Unity 中造成任何质量损失

如何使用
只需在“项目”窗口中创建一个名为“编辑器”的文件夹,然后在其中添加此脚本即可。然后,打开窗口-Convert Textures to PNG,配置参数并点击“Convert to PNG! ”。

就我而言,它已将某些 3D 资源的总文件大小从 1.08 GB 减少到 510 MB。

只要禁用“Keep Original Files”或将项目的资源序列化模式设置为“强制文本”,就会保留对转换后的纹理的引用。
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;public class ConvertTexturesToPNG : EditorWindow
{private const string DUMMY_TEXTURE_PATH = "Assets/convert_dummyy_texturee.png";private const bool REMOVE_MATTE_FROM_PSD_BY_DEFAULT = true;private readonly GUIContent[] maxTextureSizeStrings = { new GUIContent( "32" ), new GUIContent( "64" ), new GUIContent( "128" ), new GUIContent( "256" ), new GUIContent( "512" ), new GUIContent( "1024" ), new GUIContent( "2048" ), new GUIContent( "4096" ), new GUIContent( "8192" ), new GUIContent( "16384" ) };private readonly int[] maxTextureSizeValues = { 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384 };private readonly GUIContent rootPathContent = new GUIContent( "Root Path:", "Textures inside this folder (recursive) will be converted" );private readonly GUIContent textureExtensionsContent = new GUIContent( "Textures to Convert:", "Only Textures with these extensions will be converted (';' separated)" );private readonly GUIContent excludedDirectoriesContent = new GUIContent( "Excluded Directories:", "Textures inside these directories won't be converted (';' separated)" );private readonly GUIContent keepOriginalFilesContent = new GUIContent( "Keep Original Files:", "If selected, original Texture files won't be deleted after the conversion" );private readonly GUIContent maxTextureSizeContent = new GUIContent( "Max Texture Size:", "Textures larger than this size will be downscaled to this size" );private readonly GUIContent optiPNGPathContent = new GUIContent( "OptiPNG Path (Optional):", "If 'optipng.exe' is selected, it will be used to reduce the image sizes even further (roughly 20%) but the process will take more time" );private readonly GUIContent optiPNGOptimizationContent = new GUIContent( "OptiPNG Optimization:", "Determines how many trials OptiPNG will do to optimize the image sizes. As this value increases, computation time will increase exponentially" );private readonly GUIContent optiPNGURL = new GUIContent( "...", "http://optipng.sourceforge.net/" );private readonly GUILayoutOption GL_WIDTH_25 = GUILayout.Width( 25f );private string rootPath = "";private string textureExtensions = ".tga;.psd;.tiff;.tif;.bmp";private string excludedDirectories = "";private bool keepOriginalFiles = false;private int maxTextureSize = 8192;private string optiPNGPath = "";private int optiPNGOptimization = 3;private Vector2 scrollPos;[MenuItem( "Window/Convert Textures to PNG" )]private static void Init(){ConvertTexturesToPNG window = GetWindow<ConvertTexturesToPNG>();window.titleContent = new GUIContent( "Convert to PNG" );window.minSize = new Vector2( 285f, 160f );window.Show();}private void OnEnable(){// By default, Root Path points to this project's Assets folderif( string.IsNullOrEmpty( rootPath ) )rootPath = Application.dataPath;}private void OnGUI(){scrollPos = GUILayout.BeginScrollView( scrollPos );rootPath = PathField( rootPathContent, rootPath, true, "Choose target directory" );textureExtensions = EditorGUILayout.TextField( textureExtensionsContent, textureExtensions );excludedDirectories = EditorGUILayout.TextField( excludedDirectoriesContent, excludedDirectories );keepOriginalFiles = EditorGUILayout.Toggle( keepOriginalFilesContent, keepOriginalFiles );maxTextureSize = EditorGUILayout.IntPopup( maxTextureSizeContent, maxTextureSize, maxTextureSizeStrings, maxTextureSizeValues );optiPNGPath = PathField( optiPNGPathContent, optiPNGPath, false, "Choose optipng.exe path", optiPNGURL );if( !string.IsNullOrEmpty( optiPNGPath ) ){EditorGUI.indentLevel++;optiPNGOptimization = EditorGUILayout.IntSlider( optiPNGOptimizationContent, optiPNGOptimization, 2, 7 );EditorGUI.indentLevel--;}EditorGUILayout.Space();// Convert Textures to PNGif( GUILayout.Button( "Convert to PNG!" ) ){double startTime = EditorApplication.timeSinceStartup;List<string> convertedPaths = new List<string>( 128 );long originalTotalSize = 0L, convertedTotalSize = 0L, convertedTotalSizeOptiPNG = 0L;try{rootPath = rootPath.Trim();excludedDirectories = excludedDirectories.Trim();textureExtensions = textureExtensions.ToLowerInvariant().Replace( ".png", "" ).Trim();optiPNGPath = optiPNGPath.Trim();if( rootPath.Length == 0 )rootPath = Application.dataPath;if( optiPNGPath.Length > 0 && !File.Exists( optiPNGPath ) )Debug.LogWarning( "OptiPNG doesn't exist at path: " + optiPNGPath );string[] paths = FindTexturesToConvert();string pathsLengthStr = paths.Length.ToString();float progressMultiplier = paths.Length > 0 ? ( 1f / paths.Length ) : 1f;CreateDummyTexture(); // Dummy Texture is used while reading Textures' pixelsfor( int i = 0; i < paths.Length; i++ ){if( EditorUtility.DisplayCancelableProgressBar( "Please wait...", string.Concat( "Converting: ", ( i + 1 ).ToString(), "/", pathsLengthStr ), ( i + 1 ) * progressMultiplier ) )throw new Exception( "Conversion aborted" );string pngFile = Path.ChangeExtension( paths[i], ".png" );string pngMetaFile = pngFile + ".meta";string originalMetaFile = paths[i] + ".meta";bool isPSDImage = Path.GetExtension( paths[i] ).ToLowerInvariant() == ".psd";// Make sure to respect PSD assets' "Remove Matte (PSD)" optionif( isPSDImage ){bool removeMatte = REMOVE_MATTE_FROM_PSD_BY_DEFAULT;if( File.Exists( originalMetaFile ) ){const string removeMatteOption = "pSDRemoveMatte: ";string metaContents = File.ReadAllText( originalMetaFile );int removeMatteIndex = metaContents.IndexOf( removeMatteOption );if( removeMatteIndex >= 0 )removeMatte = metaContents[removeMatteIndex + removeMatteOption.Length] != '0';}SerializedProperty removeMatteProp = new SerializedObject( AssetImporter.GetAtPath( DUMMY_TEXTURE_PATH ) ).FindProperty( "m_PSDRemoveMatte" );if( removeMatteProp != null && removeMatteProp.boolValue != removeMatte ){removeMatteProp.boolValue = removeMatte;removeMatteProp.serializedObject.ApplyModifiedPropertiesWithoutUndo();}}// Temporarily copy the image file to Assets folder to create a read-write enabled Texture from itFile.Copy( paths[i], DUMMY_TEXTURE_PATH, true );AssetDatabase.ImportAsset( DUMMY_TEXTURE_PATH, ImportAssetOptions.ForceUpdate );// Convert the Texture to PNG and save itbyte[] pngBytes = AssetDatabase.LoadAssetAtPath<Texture2D>( DUMMY_TEXTURE_PATH ).EncodeToPNG();File.WriteAllBytes( pngFile, pngBytes );originalTotalSize += new FileInfo( paths[i] ).Length;convertedTotalSize += new FileInfo( pngFile ).Length;// Run OptiPNG to optimize the PNGif( optiPNGPath.Length > 0 && File.Exists( optiPNGPath ) ){try{Process.Start( new ProcessStartInfo( optiPNGPath ){Arguments = string.Concat( "-o ", optiPNGOptimization.ToString(), " \"", pngFile, "\"" ),CreateNoWindow = true,UseShellExecute = false} ).WaitForExit();}catch( Exception e ){Debug.LogException( e );}convertedTotalSizeOptiPNG += new FileInfo( pngFile ).Length;}// If .meta file exists, copy it to PNG imageif( File.Exists( originalMetaFile ) ){File.Copy( originalMetaFile, pngMetaFile, true );// Try changing original meta file's GUID to avoid collisions with PNG (Credit: https://gist.github.com/ZimM-LostPolygon/7e2f8a3e5a1be183ac19)if( keepOriginalFiles ){string metaContents = File.ReadAllText( originalMetaFile );int guidIndex = metaContents.IndexOf( "guid: " );if( guidIndex >= 0 ){string guid = metaContents.Substring( guidIndex + 6, 32 );string newGuid = Guid.NewGuid().ToString( "N" );metaContents = metaContents.Replace( guid, newGuid );File.WriteAllText( originalMetaFile, metaContents );}}// Don't show "Remote Matte (PSD)" option for converted Texturesif( isPSDImage ){string metaContents = File.ReadAllText( pngMetaFile );bool modifiedMeta = false;if( metaContents.Contains( "pSDShowRemoveMatteOption: 1" ) ){metaContents = metaContents.Replace( "pSDShowRemoveMatteOption: 1", "pSDShowRemoveMatteOption: 0" );modifiedMeta = true;}if( metaContents.Contains( "pSDRemoveMatte: 1" ) ){metaContents = metaContents.Replace( "pSDRemoveMatte: 1", "pSDRemoveMatte: 0" );modifiedMeta = true;}if( modifiedMeta )File.WriteAllText( pngMetaFile, metaContents );}}if( !keepOriginalFiles ){File.Delete( paths[i] );if( File.Exists( originalMetaFile ) )File.Delete( originalMetaFile );}convertedPaths.Add( paths[i] );}}catch( Exception e ){Debug.LogException( e );}finally{EditorUtility.ClearProgressBar();if( File.Exists( DUMMY_TEXTURE_PATH ) )AssetDatabase.DeleteAsset( DUMMY_TEXTURE_PATH );// Force Unity to import PNG images (otherwise we'd have to minimize Unity and then maximize it)AssetDatabase.Refresh();// Print information to ConsoleStringBuilder sb = new StringBuilder( 100 + convertedPaths.Count * 75 );sb.Append( "Converted " ).Append( convertedPaths.Count ).Append( " Texture(s) to PNG in " ).Append( ( EditorApplication.timeSinceStartup - startTime ).ToString( "F2" ) ).Append( " seconds (" ).Append( EditorUtility.FormatBytes( originalTotalSize ) ).Append( " -> " ).Append( EditorUtility.FormatBytes( convertedTotalSize ) );if( convertedTotalSizeOptiPNG > 0L )sb.Append( " -> " ).Append( EditorUtility.FormatBytes( convertedTotalSizeOptiPNG ) ).Append( " with OptiPNG" );sb.AppendLine( "):" );for( int i = 0; i < convertedPaths.Count; i++ )sb.Append( "- " ).AppendLine( convertedPaths[i] );Debug.Log( sb.ToString() );}}GUILayout.EndScrollView();}private string PathField( GUIContent label, string path, bool isDirectory, string title, GUIContent downloadURL = null ){GUILayout.BeginHorizontal();path = EditorGUILayout.TextField( label, path );if( GUILayout.Button( "o", GL_WIDTH_25 ) ){string selectedPath = isDirectory ? EditorUtility.OpenFolderPanel( title, "", "" ) : EditorUtility.OpenFilePanel( title, "", "exe" );if( !string.IsNullOrEmpty( selectedPath ) )path = selectedPath;GUIUtility.keyboardControl = 0; // Remove focus from active text field}if( downloadURL != null && GUILayout.Button( downloadURL, GL_WIDTH_25 ) )Application.OpenURL( downloadURL.tooltip );GUILayout.EndHorizontal();return path;}private string[] FindTexturesToConvert(){HashSet<string> texturePaths = new HashSet<string>();HashSet<string> targetExtensions = new HashSet<string>( textureExtensions.Split( ';' ) );// Get directories to excludestring[] excludedPaths = excludedDirectories.Split( ';' );for( int i = 0; i < excludedPaths.Length; i++ ){excludedPaths[i] = excludedPaths[i].Trim();if( excludedPaths[i].Length == 0 )excludedPaths[i] = "NULL/";else{excludedPaths[i] = Path.GetFullPath( excludedPaths[i] );// Make sure excluded directory paths end with directory separator charif( Directory.Exists( excludedPaths[i] ) && !excludedPaths[i].EndsWith( Path.DirectorySeparatorChar.ToString() ) )excludedPaths[i] += Path.DirectorySeparatorChar;}}// Iterate through all files in Root Pathstring[] allFiles = Directory.GetFiles( rootPath, "*.*", SearchOption.AllDirectories );for( int i = 0; i < allFiles.Length; i++ ){// Only process filtered image filesif( targetExtensions.Contains( Path.GetExtension( allFiles[i] ).ToLowerInvariant() ) ){bool isExcluded = false;if( excludedPaths.Length > 0 ){// Make sure the image file isn't part of an excluded directorystring fileFullPath = Path.GetFullPath( allFiles[i] );for( int j = 0; j < excludedPaths.Length; j++ ){if( fileFullPath.StartsWith( excludedPaths[j] ) ){isExcluded = true;break;}}}if( !isExcluded )texturePaths.Add( allFiles[i] );}}string[] result = new string[texturePaths.Count];texturePaths.CopyTo( result );return result;}// Creates dummy Texture asset that will be used to read Textures' pixelsprivate void CreateDummyTexture(){if( !File.Exists( DUMMY_TEXTURE_PATH ) ){File.WriteAllBytes( DUMMY_TEXTURE_PATH, new Texture2D( 2, 2 ).EncodeToPNG() );AssetDatabase.ImportAsset( DUMMY_TEXTURE_PATH, ImportAssetOptions.ForceUpdate );}TextureImporter textureImporter = AssetImporter.GetAtPath( DUMMY_TEXTURE_PATH ) as TextureImporter;textureImporter.maxTextureSize = maxTextureSize;textureImporter.isReadable = true;textureImporter.filterMode = FilterMode.Point;textureImporter.mipmapEnabled = false;textureImporter.alphaSource = TextureImporterAlphaSource.FromInput;textureImporter.npotScale = TextureImporterNPOTScale.None;textureImporter.textureCompression = TextureImporterCompression.Uncompressed;textureImporter.SaveAndReimport();}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/117285.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

交互式 Web 应用 0 基础入门

初探 Gradio&#xff1a;轻松构建交互式 Web 应用 文章目录 初探 Gradio&#xff1a;轻松构建交互式 Web 应用Why Gradio?安装 Gradio创建交互式界面1. gr.Interface2. gr.Blocks 强大的组件库输入输出组件控制组件布局组件 示例交互式数据可视化多组件同时&#xff08;嵌套&a…

vue3 源码解析(1)— reactive 响应式实现

前言 本文是 vue3 源码解析系列的第一篇文章&#xff0c;项目代码的整体实现是参考了 v3.2.10 版本&#xff0c;项目整体架构可以参考之前我写过的文章 rollup 实现多模块打包。话不多说&#xff0c;让我们通过一个简单例子开始这个系列的文章。 举个例子 <!DOCTYPE html…

电力通信与泛在电力物联网技术的应用与发展-安科瑞黄安南

摘要&#xff1a;随着我国社会经济的快速发展&#xff0c;我国科技实力得到了非常大的提升&#xff0c;当前互联网通信技术在社会中得到了广泛的应用。随着电力通信技术的快速发展与更新&#xff0c;泛在电力物联网建设成为电力通讯发展的重要方向。本文已泛在电力物联网系统为…

无中微子双贝塔衰变

无中微子双贝塔衰变 无中微子双贝塔衰变的原子核理论 双贝塔衰变的研究缘起 玛丽亚格佩特-梅耶&#xff08;Maria Goeppert-Mayer&#xff09;在1935年提出了双贝塔衰变的可能性埃托雷马约拉纳&#xff08;Ettore Majorana&#xff09;在1937年证明了如果中微子是否是Majorana…

PCL 透视投影变换(OpenGL)

文章目录 一、简介二、实现代码三、实现效果参考资料一、简介 在现实生活中,我们总会注意到离我们越远的东西看起来更小。这个神奇的效果被称之为透视(Perspective)。透视的效果在我们看一条无限长的高速公路或铁路时尤其明显,正如下面图片显示的这样: 由于透视的原因,平行线…

CVE-2023-46227 Apache inlong JDBC URL反序列化漏洞

项目介绍 Apache InLong&#xff08;应龙&#xff09;是一站式、全场景的海量数据集成框架&#xff0c;同时支持数据接入、数据同步和数据订阅&#xff0c;提供自动、安全、可靠和高性能的数据传输能力&#xff0c;方便业务构建基于流式的数据分析、建模和应用。 项目地址 h…

以太网链路聚合与交换机堆叠,集群

目录 以太网链路聚合 一.链路聚合的基本概念 二.链路聚合的配置 1.手工模式 2.LACP模式 系统优先级 接口优先级 最大活动接口数 活动链路选举 负载分担 负载分担模式 三.典型使用场景 交换机之间 交换机和服务器之间 交换机和堆叠系统 防火墙双机热备心跳线 四…

I/O 模型学习笔记【全面理解BIO/NIO/AIO】

文章目录 I/O 模型什么是 I/O 模型Java支持3种I/O模型BIO&#xff08;Blocking I/O&#xff09;NIO&#xff08;Non-blocking I/O&#xff09;AIO&#xff08;Asynchronous I/O&#xff09; BIO、NIO、AIO适用场景分析 java BIOJava BIO 基本介绍Java BIO 编程流程一个栗子实现…

10个基于.Net开发的Windows开源软件项目

1、基于.NET的强大软件开发工具 一个基于.Net Core构建的简单、跨平台快速开发框架。JNPF开发平台前后端封装了上千个常用类&#xff0c;方便扩展&#xff1b;集成了代码生成器&#xff0c;支持前后端业务代码生成&#xff0c;满足快速开发&#xff0c;提升工作效率&#xff1b…

动态开辟内存空间函数

文章目录 malloc函数calloc函数malloc函数和calloc函数的不同free函数realloc函数 malloc函数 参数是要开辟内存空间的大小 开辟成功则返回值为开辟空间的首地址&#xff0c;若开辟失败则返回一个空指针NULL calloc函数 第一个参数为开辟空间的元素个数&#xff0c;第二个参数…

【软件教程】如何用C++交叉编译出能在Android运行的ELF程序或so动态库

一、配置NDK交叉编译平台 1. 打开Android的官方ndk下载链接https://developer.android.com/ndk/downloads?hlzh-cn&#xff0c;下载windows 64位ndk环境包。 2. 解压后将具有以下文件的路径加入到系统环境变量。 3. 配置好环境变量&#xff0c;如下图所示&#xff0c;Path中存…

多线程进阶

多线程常见面试题 文章目录 多线程常见面试题1. 常见的锁策略1.1乐观锁&悲观锁1.2 轻量级锁&重量级锁1.3 自旋锁&挂起等待锁1.4 读写锁&普通互斥锁1.5 公平锁&非公平锁1.6可重入锁&不可重入锁 2. CAS3. Sychronized原理3.1 锁升级3.2 锁消除3.3 锁粗化…

支持多校 微信课表小程序源码 排课小程序源码 支持导入课表 情侣课表 背景设置

练手Lab课程表小程序源码是一个基于thinkphp系统进行开发的前后端分离系统。 源码功能介绍 1、情侣功能 2、情侣间留言 3、情侣间互相设置课程表背景 4、自己日、周课程表背景设置 5、教务系统课程表导入 6、导入别人分享的课表 7、导入别人分享的单课 8、多校支持 9…

AIR101 LuatOS LVGL 显示多个标签例程

屏幕资料 AIR101与屏幕连接 PC端仿真环境合宙官方PC端版本环境搭建教程 PC电脑仿真 -- sys库是标配 _G.sys require("sys") sys.taskInit(function()local cnt0lvgl.init(480,320)--lvgl初始化local cont lvgl.cont_create(nil, nil);-- lvgl.cont_set_fit(cont, …

Centos8 降低gcc版本至gcc-7.3

1 首先卸载系统中的gcc sudo yum remove gcc 2 重新安装gcc-7.3 sudo dnf group install “Development Tools” 然后再次卸载gcc sudo yum remove gcc 然后发现centos-release-scl-rh已经安装了 sudo yum install centos-release-scl-rh yum -y install devtoolset-7-gcc dev…

不希望你的数据在云中?关闭iPhone或Mac上的iCloud

​如果你不想使用iCloud&#xff0c;可以很容易地从设备设置中选择退出并关闭它。当你禁用iCloud时&#xff0c;它会删除该设备对iCloud的访问&#xff0c;但不会删除苹果服务器上的任何数据。我们将在本文末尾向你展示如何做到这一点。 注销iCloud并完全禁用它 如果你根本不…

HDR图像处理软件 Photomatix Pro mac中文版新增功能

Photomatix Pro mac是一款专业的HDR合成软件&#xff0c;可以将不同曝光的多张照片合成为一张照片&#xff0c;而保留更多的细节。并且合成时可以帮助去除照片中的鬼影。Photomatix Pro提供两种类型的过程来增加动态范围&#xff0c;一个过程称为HDR色调映射&#xff0c;另一个…

YOLOv5算法改进(20)— 如何去写YOLOv5相关的论文(包括论文阅读+规律总结+写作方法)

前言:Hello大家好,我是小哥谈。最近一直在阅读关于YOLOv5的相关论文,读着读着我发现一条可以发论文的规律,特此简单总结一下,希望能够对同学们有所启迪!🌈 前期回顾: YOLOv5算法改进(1)— 如何去改进YOLOv5算法

kafka入门03——简单实战

目录 安装Java 安装Zookeeper 安装Kafka 生产与消费 主要是记录下Kafka的安装配置过程&#xff0c;前置条件需要安装jdk和zookeeper。 安装Java 1.Oracle官网下载对应jdk安装包 官网地址&#xff1a;Java Downloads | Oracle 好人分享了下载需要的oracle账号&#xff0c…

WPS中图的自动编号及引用

WPS中图的自动编号及引用 图的自动编号图编号的引用图编号及引用的更新 图的自动编号 将光标放置在需要插入编号的位置点击“引用”→“题注”&#xff1a; 点击“引用”→“题注”&#xff1a; 点击“编号”&#xff0c;设置图的编号格式&#xff0c;可勾选“包含章节编号”&…