截屏,截图
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.
1 IEnumerator Cap() 2 { 3 yield return new WaitForEndOfFrame(); 4 5 Texture2D tex = new Texture2D(Screen.width, Screen.height); 6 7 tex.ReadPixels(new Rect(Vector2.zero, new Vector2(Screen.width, Screen.height)), 0, 0); 8 var raw = tex.EncodeToPNG(); 9 string filename = Application.dataPath + "/111.png"; 10 11 System.IO.File.WriteAllBytes( filename, raw); 12 }
3.......摄像机可以Render到RenderTexture 然后执行保存相关操作