using UnityEditor; using UnityEngine; public static class ResCompression { [MenuItem("Tools/Res Compre")] public static void ResCompres() { CompreImages(); } private static void CompreImages() { string[] textureGuids = AssetDatabase.FindAssets("t:Texture2D", new[] { "Assets" }); int changedCount = 0; try { for (int i = 0; i < textureGuids.Length; i++) { string assetPath = AssetDatabase.GUIDToAssetPath(textureGuids[i]); EditorUtility.DisplayProgressBar( "Compress Images", assetPath, textureGuids.Length == 0 ? 1f : (float)i / textureGuids.Length); TextureImporter importer = AssetImporter.GetAtPath(assetPath) as TextureImporter; if (importer == null) { continue; } importer.GetSourceTextureWidthAndHeight(out int width, out int height); int maxSize = GetLowerPowerOfTwoMaxSize(Mathf.Max(width, height)); bool changed = false; if (importer.mipmapEnabled) { importer.mipmapEnabled = false; changed = true; } TextureImporterPlatformSettings iosSettings = importer.GetPlatformTextureSettings("iPhone"); if (!iosSettings.overridden || iosSettings.format != TextureImporterFormat.ASTC_6x6 || iosSettings.maxTextureSize != maxSize) { iosSettings.name = "iPhone"; iosSettings.overridden = true; iosSettings.format = TextureImporterFormat.ASTC_6x6; iosSettings.maxTextureSize = maxSize; importer.SetPlatformTextureSettings(iosSettings); changed = true; } if (changed) { importer.SaveAndReimport(); changedCount++; } } } finally { EditorUtility.ClearProgressBar(); } Debug.Log($"Image compression completed. Scanned {textureGuids.Length} textures, " + $"updated {changedCount} textures."); } private static int GetLowerPowerOfTwoMaxSize(int sourceSize) { const int minSize = 32; const int maxSize = 1024; if (sourceSize <= minSize) { return minSize; } int size = minSize; while (size <= maxSize / 2 && size * 2 <= sourceSize) { size *= 2; } return size; } }