Unity使用RenderTexture进行截图
发表于2018-09-07
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;
}
