| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- 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;
- }
- }
|