51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
|
|
using UnityEngine;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
|
|||
|
|
public class BatchTrimAndSaveImages : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
public Texture2D[] textures;
|
|||
|
|
public string outputPath = "Assets/OutputImages";
|
|||
|
|
|
|||
|
|
private void Start()
|
|||
|
|
{
|
|||
|
|
CropTextures();
|
|||
|
|
}
|
|||
|
|
void CropTextures()
|
|||
|
|
{
|
|||
|
|
if (textures == null || textures.Length == 0) return;
|
|||
|
|
|
|||
|
|
foreach (var texture in textures)
|
|||
|
|
{
|
|||
|
|
var originalWidth = texture.width;
|
|||
|
|
var originalHeight = texture.height;
|
|||
|
|
|
|||
|
|
// ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD>Ⱥ߶<CDB8><DFB6>Dz<EFBFBD><C7B2>к<EFBFBD><D0BA>ߴ<EFBFBD><DFB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
var width = Mathf.ClosestPowerOfTwo(originalWidth);
|
|||
|
|
var height = Mathf.ClosestPowerOfTwo(originalHeight);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>µ<EFBFBD>Texture2D<32><44><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD>ԭʼ<D4AD><CABC><EFBFBD><EFBFBD>
|
|||
|
|
var croppedTexture = new Texture2D(width, height);
|
|||
|
|
for (var y = 0; y < height; y++)
|
|||
|
|
{
|
|||
|
|
for (var x = 0; x < width; x++)
|
|||
|
|
{
|
|||
|
|
var color = texture.GetPixel((int)(x * (originalWidth / (float)width)), (int)(y * (originalHeight / (float)height)));
|
|||
|
|
croppedTexture.SetPixel(x, y, color);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Ӧ<><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
croppedTexture.Apply();
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ü<EFBFBD><C3BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
var pngData = croppedTexture.EncodeToPNG();
|
|||
|
|
File.WriteAllBytes(Path.Combine(outputPath, texture.name + ".png"), pngData);
|
|||
|
|
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
|
|||
|
|
DestroyImmediate(croppedTexture);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|