Unity使用RenderTexture进行截图

发表于2018-09-07
评论0 4.2k浏览
RenderTexture是可以被渲染的纹理,简称渲染纹理。可以应用到游戏截图,背景模糊等方面,下面要给大家介绍的就是使用RenderTexture进行截图。

代码如下:
private IEnumerator cutScreen()
    {
        RenderTexture rt = new RenderTexture(Screen.width, Screen.height, 24);
        m_mainCam.targetTexture = rt;
        m_mainCam.Render();
        m_auxCam.targetTexture = rt;
        m_auxCam.Render();
        m_nguiCam.targetTexture = rt;
        m_nguiCam.Render();
        RenderTexture.active = rt;
        Texture2D tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
        tex.ReadPixels(new Rect(0,0,Screen.width,Screen.height),0,0);
        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;
        yield return null;
    }

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引