using UnityEngine; using UnityEditor; using System.IO; public class ResizeTexturesTool : EditorWindow { private string folderPath = "Assets/"; // Default folder path private int width = 512; // Default width private int height = 512; // Default height [MenuItem("Window/Damarjian/Resize Textures Tool")] public static void ShowWindow() { GetWindow(typeof(ResizeTexturesTool)); // Create or focus the editor window } private void OnGUI() { GUILayout.Label("Select Folder and Resize Textures", EditorStyles.boldLabel); // Label EditorGUILayout.BeginHorizontal(); folderPath = EditorGUILayout.TextField("Folder Path", folderPath); // Folder path input field if (GUILayout.Button("Browse")) // Browse button { folderPath = EditorUtility.OpenFolderPanel("Select Folder", folderPath, ""); // Open folder dialog } EditorGUILayout.EndHorizontal(); width = EditorGUILayout.IntField("Width", width); // Width input field height = EditorGUILayout.IntField("Height", height); // Height input field if (GUILayout.Button("Resize Textures")) // Resize button { ResizeAllTexturesInFolder(); // Call resize method } } private void ResizeAllTexturesInFolder() { string[] fileEntries = Directory.GetFiles(folderPath); // Get files in the folder foreach (string fileName in fileEntries) { string assetPath = fileName.Replace(Application.dataPath, "Assets"); // Convert path Texture2D texture = AssetDatabase.LoadAssetAtPath(assetPath); // Load texture if (texture != null && !AssetDatabase.IsSubAsset(texture)) // Check if texture is valid { string path = AssetDatabase.GetAssetPath(texture); // Get asset path Texture2D resizedTexture = Resize(texture, width, height); // Resize texture byte[] bytes = resizedTexture.EncodeToPNG(); // Encode to PNG File.WriteAllBytes(path, bytes); // Write to file Debug.Log("Resized: " + assetPath); // Log message AssetDatabase.ImportAsset(path); // Re-import asset } } AssetDatabase.Refresh(); // Refresh the asset database } private Texture2D Resize(Texture2D texture, int newWidth, int newHeight) { Texture2D result = new Texture2D(newWidth, newHeight, TextureFormat.RGBA32, false); // Explicitly set format to RGBA32 // Ensure the original texture is readable string path = AssetDatabase.GetAssetPath(texture); TextureImporter ti = (TextureImporter)TextureImporter.GetAtPath(path); ti.isReadable = true; ti.SaveAndReimport(); // Create a temporary render texture RenderTexture rt = RenderTexture.GetTemporary(newWidth, newHeight); rt.filterMode = FilterMode.Bilinear; // Set the current active render texture RenderTexture.active = rt; // Blit the original texture to the render texture Graphics.Blit(texture, rt); // Read the pixels from the render texture into the result texture result.ReadPixels(new Rect(0, 0, newWidth, newHeight), 0, 0); result.Apply(); // Reset the active render texture RenderTexture.active = null; // Release the temporary render texture RenderTexture.ReleaseTemporary(rt); // Return the resized texture return result; } }