Unity使用RenderTexture进行截屏(海报截图分享、适配各种尺寸比例的屏幕)

74 篇文章 29 订阅

 Unity截屏分享功能,需要实现截屏功能。

美术那边会出一张海报图,尺寸为1280x720,包装成预设,预设上还有叠加一些其他UI,比如头像、昵称、二维码、宣传语、logo等等。

如果把海报图铺满全屏,那么如果手机屏幕宽高比不是1280:720,那么海报图就会被拉伸变形,如果等比例缩放海报图,那海报图就会有部分超出屏幕外,无法被截屏到。

解决办法:

海报图示例化后不缩放不拉伸,如果是在1280:720的宽高比的手机上,就刚好铺满全屏,如果是其他宽高比的屏幕,则海报图四周有可能会有留空,没有铺满全屏。没关系,截屏的时候,只读取海报图所在的像素位置即可,这里需要进行一些换算,具体看下面代码的region 2的部分的逻辑

 

public void cutScreen()
{
    RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 24);
    // 赋值各个摄像机的targetTexture,注意摄像机的渲染顺序
    // m_mainCam、m_auxCam、m_nguiCam是我这边定义的三个摄像机
    m_mainCam.targetTexture = rt;
    m_mainCam.Render();
    m_auxCam.targetTexture = rt;
    m_auxCam.Render();
    m_nguiCam.targetTexture = rt;
    m_nguiCam.Render();
    RenderTexture.active = rt;
    
    
    #region 1
    // 全屏读取像素
    //Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
    //tex.ReadPixels(new Rect(0,0,Screen.width,Screen.height),0,0);
    #endretion 1

    #region 2
    // 读取1280x720宽高比的像素,适用于1280x720宽高比的海报截屏分享
    float factor_x = Screen.width * 1.0f / Screen.height/ (1280 * 1.0f / 720);
    factor_x = factor_x < 1.0f ? 1.0f : factor_x;
    float factor_y = 1280 * 1.0f / 720 / (Screen.width * 1.0f / Screen.height);
    factor_y = factor_y < 1.0f ? 1.0f : factor_y;    

    Texture2D tex = new Texture2D((int)(width / factor_x), (int)(height / factor_y), TextureFormat.RGB24, false);
    tex.ReadPixels(new Rect((width - width / factor_x) / 2, (height - height / factor_y) / 2f, width / factor_x, height / factor_y), 0, 0);
    #endregion 2 

    tex.Apply();
    RenderTexture.active = null;
    m_mainCam.targetTexture = null;
    m_auxCam.targetTexture = null;
    m_nguiCam.targetTexture = null;
    Destroy(rt);

    GameObject photo = new GameObject("photo");
    UITexture uiTex = photo.AddComponent<UITexture>();
    uiTex .mainTexture = tex;

    photo.transform.parent = m_gamePanel.transform;
    photo.transform.localPosition = new Vector3();
    photo.transform.localScale = Vector3.one;
    uiTex.width = 611;
    uiTex.height = 344;
}

 

Unity中,RenderTexture是一个用于渲染游戏场景到内存缓冲区的组件,这对于实时处理、后期处理效果或者将游戏画面保存为图片非常有用。创建RenderTexture的基本步骤如下: 1. 导入RenderTexture组件:首先需要导入`UnityEngine.Rendering`命名空间,它包含了RenderTexture的类。 ```csharp using UnityEngine.Rendering; ``` 2. 创建RenderTexture实例:在你的脚本里,可以使用`new RenderTexture(width, height, format)`来创建一个新的RenderTexture,其中width和height是纹理的尺寸,format是格式类型,如`RenderTextureFormat.RGBA32Bit`或`RenderTextureFormat.ARGBHalf`等。 ```csharp int width = 512; int height = 512; RenderTexture renderTex = new RenderTexture(width, height, RenderTextureFormat.RGBA32Bit); ``` 3. 设置RenderTexture作为相机目标:如果你想从某个相机渲染内容到这个纹理,需要将相机的目标设置为RenderTexture。 ```csharp Camera camera = Camera.main; camera.targetTexture = renderTex; ``` 4. 渲染帧:调用相机的`Render()`方法来实际进行渲染,这一步通常放在Update()函数或其他合适的时间点。 ```csharp camera.Render(); ``` 5. 使用RenderTexture:一旦内容渲染完成,你可以将其读取为Texture2D,然后用于UI显示、保存为文件或者进一步处理。 ```csharp Texture2D tex = new Texture2D(width, height, TextureFormat.RGB24, false); tex.ReadPixels(new Rect(0, 0, width, height), 0, 0); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林新发

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

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

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

打赏作者

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

抵扣说明:

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

余额充值