【Unity100个实用小技巧】保证原图片的情况下,动态扩展图片尺寸

文章介绍了Unity这一实时3D开发平台,特别讨论了在图片分辨率动态扩展时保持图片质量的两种方法:一种利用GL进行矩阵操作,另一种通过RenderTexture进行动态调整。这两种方案旨在确保在改变分辨率时不丢失图像原有的清晰度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • ☀️博客主页:CSDN博客主页
  • 💨本文由 萌萌的小木屋 原创,首发于 CSDN💢
  • 🔥学习专栏推荐:面试汇总
  • ❗️游戏框架专栏推荐:游戏实用框架专栏
  • ⛅️点赞 👍 收藏 ⭐留言 📝,如有错误请指正
  • 📆 未来很长,值得我们全力奔赴更美好的生活✨

  • ------------------❤️分割线❤️-------------------------

请添加图片描述请添加图片描述请添加图片描述

Unity 小科普

老规矩,先介绍一下Unity的科普小知识:

  •  Unity 是行业领先的实时3D开发平台。
  • 包括游戏开发,电影,AR/VR,虚拟现实在内的所有创作者,可以将梦想照进现实。
  • Unity提供了一套完整完善的软件解决方案,可用于创作,运营和模拟任何2D和3D的内容,进本全平台支持
  • 实际上Unity就是一个游戏引擎,大名鼎鼎的原神就是用它制作的。

Unity100个实用小技巧

需求:图片分辨率动态扩展的时候,如何保持扩展了分辨率,又不丢失原本的图片。

两种方案

第第一种:GL实现

    public Texture2D ReSetTextureSize(Texture2D tex, int width, int height)
        {
            var rendTex = new RenderTexture(width, height, 48, RenderTextureFormat.Default);

            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.RGBA64, false);

            RenderTexture.active = rendTex;

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

            finalTex.Apply();

            return finalTex;
        }

第二种:RenderTexture实现

        private Texture2D ResizeTexture(Texture2D source, int width, int height)
        {
            if (source != null)
            {
                // 创建临时的RenderTexture
                RenderTexture renderTex = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear,1);
                // 复制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);
                GameObject.Destroy(source);
                return resizedTexture;
            }
            else
            {
                return null;
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是小狼君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值