using System.IO; // 用于文件操作 using UnityEngine; public class SaveTexture { public Texture2D clippedTexture; // 需要保存的 Texture2D public static void SaveTextureAsPNG(Texture2D texture, string filePath) { // 将 Texture2D 编码为 PNG 格式的字节数组 byte[] bytes = texture.EncodeToPNG(); // 将字节写入文件 File.WriteAllBytes(filePath, bytes); Debug.Log($"Texture saved as PNG at: {filePath}"); } // 将 Texture2D 放大到指定尺寸 public static Texture2D ResizeTexture(Texture2D sourceTexture, int width, int height) { // 创建一个新的 Texture2D,目标尺寸为 width x height Texture2D resizedTexture = new Texture2D(width, height, sourceTexture.format, false); // 逐像素采样并填充新的 Texture2D for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // 计算采样位置(将目标尺寸映射回原始尺寸) float u = (float)x / (float)width; float v = (float)y / (float)height; // 获取原始纹理的颜色 Color color = sourceTexture.GetPixelBilinear(u, v); // 设置到新的纹理中 resizedTexture.SetPixel(x, y, color); } } // 应用更改 resizedTexture.Apply(); return resizedTexture; } }