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

这篇具有很好参考价值的文章主要介绍了[Unity]将所有 TGA、TIFF、PSD 和 BMP(可自定义)纹理转换为 PNG,以减小项目大小,而不会在 Unity 中造成任何质量损失。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

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

只要禁用“Keep Original Files”或将项目的资源序列化模式设置为“强制文本”,就会保留对转换后的纹理的引用。
tga输出为png,1024程序员节,unity,游戏引擎,ui,c#文章来源地址https://www.toymoban.com/news/detail-786178.html

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 folder
		if( 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 PNG
		if( 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' pixels

				for( 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)" option
					if( 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 it
					File.Copy( paths[i], DUMMY_TEXTURE_PATH, true );
					AssetDatabase.ImportAsset( DUMMY_TEXTURE_PATH, ImportAssetOptions.ForceUpdate );

					// Convert the Texture to PNG and save it
					byte[] 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 PNG
					if( 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 image
					if( 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 Textures
						if( 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 Console
				StringBuilder 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 exclude
		string[] 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 char
				if( Directory.Exists( excludedPaths[i] ) && !excludedPaths[i].EndsWith( Path.DirectorySeparatorChar.ToString() ) )
					excludedPaths[i] += Path.DirectorySeparatorChar;
			}
		}

		// Iterate through all files in Root Path
		string[] allFiles = Directory.GetFiles( rootPath, "*.*", SearchOption.AllDirectories );
		for( int i = 0; i < allFiles.Length; i++ )
		{
			// Only process filtered image files
			if( 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 directory
					string 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' pixels
	private 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();
	}
}

到了这里,关于[Unity]将所有 TGA、TIFF、PSD 和 BMP(可自定义)纹理转换为 PNG,以减小项目大小,而不会在 Unity 中造成任何质量损失的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【GIS】Python多线程转换NC格式文件为TIFF

    【GIS】使用cdsapi下载ERA5和ERA5_land逐小时数据 NC文件读取使用netCDF4,NC文件转换为TIF使用rasterio或者GDAL。 格点数据转换为TIFF文件时候,计算六参数时候,应该要考虑,格点数据存储的坐标属于栅格中心点的位置,转换为TIFF时候,可能需要考虑栅格大小。

    2024年02月12日
    浏览(36)
  • 【项目】用户可自定义简易宏键盘

    在日常的电脑使用中,为了节省操作的时间,我们常常会使用电脑内置的快捷键来替代繁琐的鼠标操作,如使用ctrl+c来复制、ctrl+v来粘贴、ctrl+z来撤回。 但即使是像上面效率非常高的快捷键,也要用户同时按下两个按键才能实现,更不用说有一些键在键盘之间的距离比较远,

    2024年02月09日
    浏览(29)
  • python:转换维度、reshape、灰度拉伸、矩阵线性插值、gdal读取tiff图

    【Pnet原型网络】【】  【灰度拉伸】  【对矩阵做线性插值】 【 Totensor()函数 】 ToTensor()将shape为(H, W, C)的nump.ndarray或img转为shape为(C, H, W)的tensor, 其将每一个数值归一化到[0,1] ,其归一化方法比较简单,直接除以255即可。具体可参见如下代码: import torchvision.transforms as tr

    2023年04月08日
    浏览(40)
  • 【Unity编辑器扩展】(一)PSD转UGUI Prefab, Aspose.PSD和Harmony库的使用

    【Unity编辑器扩展】(二)PSD转UGUI Prefab, 图层解析和碎图导出_psd导入unity_TopGames的博客-CSDN博客 【Unity编辑器扩展】(三)PSD转UGUI Prefab, 一键拼UI/同步字体/自动9宫切图(完结篇)_TopGames的博客-CSDN博客 解放UI程序/美术? psd文件一键转ui prefab 支持所有ui类型 支持textmeshpro psd一键转ugui

    2024年02月05日
    浏览(36)
  • 用C语言实现定积分计算(包括无穷积分/可自定义精度)

    关于严谨性的声明: 在用C语言进行定积分的计算之前,我需要声明以下几点: 一、我们所进行定积分计算的函数都是应当是黎曼可积的,这保证了我们即使均匀地分割区间也保证了积分的收敛性。 二、我们同时还应该认识到,鉴于某些函数不一定是在每一点上都是可导的,

    2024年02月13日
    浏览(39)
  • Bootstrap select2之下拉框可自定义输入和选择

    1. 引入css文件 2. 引入js文件 3. select标签引入class 我是在项目搜索框部分要加一个下拉框,本来甚至不需要上面那么多引入,都可以有一个下拉框,但是要求点击可以出现输入框自定义输入来筛选,然后想到了这个。但是想要利用表单来实现,因为不是一种表单,所以样式混乱

    2024年02月05日
    浏览(41)
  • 哔哩哔哩 B站 bilibili 视频倍速设置 视频倍速可自定义

    目录 一、复制如下代码 二、在B站视频播放页面进入控制台 (一)以火狐浏览器为例          (二)以谷歌浏览器为例 三、将复制的代码粘贴到下方输入框,并 回车Enter 即可 (一)以火狐浏览器为例          (二)以谷歌浏览器为例  四、然后就可以了 (该代码用于

    2024年02月11日
    浏览(42)
  • Vue+Element Progress 进度条显示文字 %修改,使用format方法显示文字可自定义

    要实现这样子的 将进度条里显示的文字 后的 % 去掉 主要用到 format 方法

    2024年02月09日
    浏览(47)
  • 快快销ShopMatrix 分销商城多端uniapp可编译5端 - 升级申请(可自定义申请表单)

      在企业或组织中,升级申请通常涉及到员工职位、权限、设备或者其他资源的提升或更新。创建一个可自定义的升级申请表单可以帮助更高效地收集和处理这类申请信息。以下是一个基本的步骤: 确定表单字段 : 申请人信息:姓名、部门、职位、联系方式等。 升级类别:

    2024年01月20日
    浏览(39)
  • 【Unity编辑器扩展】(三)PSD转UGUI Prefab, 一键拼UI解放美术/程序(完结)

    解放UI程序/美术? psd文件一键转ui prefab 支持所有ui类型 支持textmeshpro psd一键转ugui prefab工具 设计原理和详细使用方法 第一步,把psd图层转换为可编辑的节点树,并自动解析UI类型、自动绑定UI子元素:  第二步, 点击“生成UIForm\\\"按钮生成UI预制体 (若有UI类型遗漏可在下拉菜单

    2024年02月13日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包