🌮Follow me
public static Texture2D ScaleTexture(this Texture2D tex, int targetHeight) {
int width = tex.width;
int height = tex.height;
float whratio = width * 1.0f / height;
if (height > targetHeight)
{
height = targetHeight;
width = (int)(height * whratio);
Debug.Log($"重置宽高:{tex.width}_{tex.height} => {width}_{height}");
}
return tex.ScaleTexture(width,height);
}
public static Texture2D ScaleTexture(this Texture2D tex, int targetWidth, int targetHeight)
{
Texture2D res = new Texture2D(targetWidth, targetHeight, tex.format, true);
Color[] pixels = res.GetPixels(0);
float x = ((float)1 / tex.width) * ((float)tex.width / targetWidth);
float y = ((float)1 / tex.height) * ((float)tex.height / targetHeight);
for (int px = 0; px < pixels.Length; px++)
{
pixels[px] = tex.GetPixelBilinear(x * ((float)px % targetWidth), y * ((float)Mathf.Floor(px / targetWidth)));
}
res.SetPixels(pixels, 0);
res.Apply();
return res;
}
|