首先推荐一篇文章,ASTC纹理压缩格式详解,里面详细讲述了iOS,android不同的贴图压缩方式与各种压缩结果对比。
转java开发后,好久没有接触过unity,回老家后为混口饭,又重新拿起了unity工作。最近接手一款休闲款word类游戏,里面包含无数UI与贴图,为控制包体,内存等,尝试压缩贴图,无意之间找到这篇文章,拜读之后感觉很受用,就拿到了项目中。但是在贴图导入项目中,unity会自动压缩图片,但压缩类型不是我想要的,然后android平台设置一遍之后,又要到iOS平台设置一遍,作为懒人一个,就做了这么一个小工具,它会在贴图导入项目后,自动设置图片压缩格式,图集等。(参考他人代码,略作改动)
using System; using System.Collections.Generic; using System.IO; using System.Reflection; using UnityEditor; using UnityEngine; namespace ETHotfix { public class TextureImportSetting: AssetPostprocessor { private static readonly int[] maxSizes = { 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192 }; private struct TextureImporterInfo { public readonly TextureImporterFormat textureImporterFormat; public readonly string packingTag; public TextureImporterInfo(TextureImporterFormat formatInfo, string packingTag) { textureImporterFormat = formatInfo; this.packingTag = packingTag; } } private readonly Dictionary<string, TextureImporterInfo> TextureImporterFormatsDict = new Dictionary<string, TextureImporterInfo> { { "Bonus", new TextureImporterInfo(TextureImporterFormat.ASTC_RGBA_6x6, "Bonus") }, { "Comm", new TextureImporterInfo(TextureImporterFormat.RGBA32, "Comm") }, { "Events", new TextureImporterInfo(TextureImporterFormat.ASTC_RGB_6x6, "Events") }, { "StorePack", new TextureImporterInfo(TextureImporterFormat.ASTC_RGB_6x6, "StorePack") }, { "Store.Bg", new TextureImporterInfo(TextureImporterFormat.ASTC_RGB_8x8, "Store.Bg") } }; private static readonly string DEFAULTS_KEY = "DEFAULTS_DONE"; private static readonly uint DEFAULTS_VERSION = 2; private bool IsAssetProcessed { get { string key = $"{DEFAULTS_KEY}_{DEFAULTS_VERSION}"; return assetImporter.userData.Contains(key); } set { string key = $"{DEFAULTS_KEY}_{DEFAULTS_VERSION}"; assetImporter.userData = value? key : string.Empty; } } private void OnPreprocessTexture() { if (IsAssetProcessed) { return; } IsAssetProcessed = true; if (!assetPath.EndsWith(".jpg") && !assetPath.EndsWith(".png")) { return; } if (assetPath.IndexOf("Bundles", StringComparison.Ordinal) < 0) { return; } TextureImporter textureImporter = (TextureImporter) assetImporter; if (textureImporter == null) { return; } textureImporter.textureType = TextureImporterType.Sprite; int width = 0, height = 0; GetOriginalSize(textureImporter, out width, out height); bool IsPowerOfTwo = GetIsPowerOfTwo(width) && GetIsPowerOfTwo(height); if (!IsPowerOfTwo) { textureImporter.npotScale = TextureImporterNPOTScale.None; } textureImporter.mipmapEnabled = false; int maxTextureSize = GetMaxSize(Mathf.Max(width, height)); string dirName = new DirectoryInfo(Path.GetDirectoryName(assetPath)).Name; TextureImporterPlatformSettings textureImporterPlatformSettings = GetTextureImporterPlatformSettings(maxTextureSize, dirName); textureImporter.SetPlatformTextureSettings(textureImporterPlatformSettings); if (TextureImporterFormatsDict.ContainsKey(dirName)) { textureImporter.spritePackingTag = TextureImporterFormatsDict[dirName].packingTag; } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } private TextureImporterPlatformSettings GetTextureImporterPlatformSettings(int size, string dirName) { TextureImporterPlatformSettings textureImporterPlatformSettings = new TextureImporterPlatformSettings(); textureImporterPlatformSettings.androidETC2FallbackOverride = AndroidETC2FallbackOverride.UseBuildSettings; textureImporterPlatformSettings.maxTextureSize = GetMaxSize(size); textureImporterPlatformSettings.resizeAlgorithm = TextureResizeAlgorithm.Mitchell; textureImporterPlatformSettings.overridden = true; textureImporterPlatformSettings.compressionQuality = 1; #if UNITY_ANDROID textureImporterPlatformSettings.name = "Android"; #elif UNITY_IOS textureImporterPlatformSettings.name = "iOS"; #endif TextureImporterFormat formatType = TextureImporterFormat.ASTC_RGBA_6x6; if (TextureImporterFormatsDict.ContainsKey(dirName)) { formatType = TextureImporterFormatsDict[dirName].textureImporterFormat; } textureImporterPlatformSettings.format = formatType; return textureImporterPlatformSettings; } private void GetOriginalSize(TextureImporter importer, out int width, out int height) { object[] args = new object[2] { 0, 0 }; MethodInfo mi = typeof (TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance); mi.Invoke(importer, args); width = (int) args[0]; height = (int) args[1]; } private bool GetIsPowerOfTwo(int f) { return (f & (f - 1)) == 0; } private int GetMaxSize(int longerSize) { int index = 0; for (int i = 0; i < maxSizes.Length; i++) { if (longerSize <= maxSizes[i]) { index = i; break; } } return maxSizes[index]; } } }
原文:https://www.cnblogs.com/Yellow0-0River/p/14271191.html