RenderTexture截图

1.原理

通过Texture2D的ReadPixels方法将读取RenderTexture数据,据此创建一个Sprite。

2.代码

//RenderTexture
public RenderTexture mainPadTexture;
//要填充的Image
public Image screenTexturImage;
//读取大小
private Rect screenShotSize;

public void GetASnap()
{
    //获取大小
    screenShotSize = new Rect();
    screenShotSize.x = 0;
    screenShotSize.y = 0;
    screenShotSize.width = mainPadTexture.width;
    screenShotSize.height = mainPadTexture.height;

    //if (this.gameObject.activeSelf)
    {
        //激活当前的渲染贴图
        RenderTexture.active = mainPadTexture;
        //创建2d纹理
        Texture2D screenShot = new Texture2D((int)screenShotSize.width, (int)screenShotSize.height, TextureFormat.RGB24, false);
        //读取
        screenShot.ReadPixels(screenShotSize, 0, 0);
        screenShot.Apply();
        //创建精灵
        Sprite spr = Sprite.Create(screenShot, screenShotSize, Vector2.zero);
        screenTexturImage.sprite = spr;
    }
}

RenderTexture太暗

在RenderTexture的Inspector面板中,勾选sRGB(Color RenderTexture)选项。

吐槽 littleplus

*

*

2位绅士参与评论

  1. Sparrow12-06 01:16 回复

    首图片是unity酱?

  2. littleplus11-20 13:25 回复

    好久没来兔子的博客,幸好兔子也很久没更新了(笑)