Unity截屏
截屏,截图
1……使用Application 提供的api
Application.CaptureScreenshot(Application.dataPath + "/11.png");
2…..利用Texture2D提供的函数 ReadPixels,注意要在每帧渲染完成后才能使用,
否则会抛出异常: ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame.,该函数是从 读取的 RenderTexture. active。下面是文档说明
This will copy a rectangular pixel area from the currently active RenderTexture or the view (specified by the source
parameter) into the position defined by destX
anddestY
. Both coordinates use pixel space – (0,0) is lower left.
IEnumerator Cap() { yield return new WaitForEndOfFrame(); Texture2D tex = new Texture2D(Screen.width, Screen.height); tex.ReadPixels(new Rect(Vector2.zero, new Vector2(Screen.width, Screen.height)), 0, 0); var raw = tex.EncodeToPNG(); string filename = Application.dataPath + "/111.png"; System.IO.File.WriteAllBytes( filename, raw); }
3…….摄像机可以Render到RenderTexture 然后执行保存相关操作