通过利用RenderTexture来创建新的大小的Texture。
需要注意的是,不需要再用到的Texture在修改完尺寸后应该及时销毁,释放资源。
方法如下:
private static Texture2D ResizeTexture(Texture2D source, int width, int height)
{
if (source != null)
{
// 创建临时的RenderTexture
RenderTexture renderTex = RenderTexture.GetTemporary(width, height, 24, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
// 复制source的纹理到RenderTexture里
Graphics.Blit(source, renderTex);
// 开启当前RenderTexture激活状态
RenderTexture previous = RenderTexture.active;
RenderTexture.active = renderTex;
// 创建修改后的纹理,保持与源纹理相同压缩格式
Texture2D resizedTexture = new Texture2D(width, height, source.format, false);
// 读取像素到创建的纹理中
resizedTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
// 应用修改到GPU上
resizedTexture.Apply();
// 停止当前RenderTexture工作
RenderTexture.active = previous;
// 释放内存
RenderTexture.ReleaseTemporary(renderTex);
return resizedTexture;
}
else
{
return null;
}
}