unity改变图片像素大小_Unity使用C#不失真修改图片的分辨率

这段代码展示了如何在Unity中通过创建RenderTexture并应用图形操作来改变Texture2D对象的尺寸。它涉及GL指令、材质设置以及最终将RenderTexture的内容复制到新的Texture2D对象上。
摘要由CSDN通过智能技术生成

public static Texture2D ReSetTextureSize(Texture2D tex, int width, int height)

{

var rendTex = new RenderTexture(width, height, 24, RenderTextureFormat.ARGB32);

rendTex.Create();

Graphics.SetRenderTarget(rendTex);

GL.PushMatrix();

GL.Clear(true, true, Color.clear);

GL.PopMatrix();

var mat = new Material(Shader.Find("Unlit/Transparent"));

mat.mainTexture = tex;

Graphics.SetRenderTarget(rendTex);

GL.PushMatrix();

GL.LoadOrtho();

mat.SetPass(0);

GL.Begin(GL.QUADS);

GL.TexCoord2(0, 0);

GL.Vertex3(0, 0, 0);

GL.TexCoord2(0, 1);

GL.Vertex3(0, 1, 0);

GL.TexCoord2(1, 1);

GL.Vertex3(1, 1, 0);

GL.TexCoord2(1, 0);

GL.Vertex3(1, 0, 0);

GL.End();

GL.PopMatrix();

var finalTex = new Texture2D(rendTex.width, rendTex.height, TextureFormat.ARGB32, false);

RenderTexture.active = rendTex;

finalTex.ReadPixels(new Rect(0, 0, finalTex.width, finalTex.height), 0, 0);

finalTex.Apply();

return finalTex;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值