Advertisement
Guest User

readpixel multiple grames

a guest
Dec 4th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. IEnumerator TakeScreenshotAndSaveCoroutine(int width, int height, Camera screenshotCamera, string saveToFileName)
  2. {
  3. yield return new WaitForEndOfFrame();
  4.  
  5. if (width <= 0 || height <= 0)
  6. {
  7. yield return null;
  8. }
  9.  
  10. if (screenshotCamera == null) screenshotCamera = Camera.main;
  11.  
  12. Texture2D screenshot = new Texture2D(width, height, TextureFormat.RGB24, false);
  13. RenderTexture renderTex = new RenderTexture(width, height, 24);
  14. screenshotCamera.targetTexture = renderTex;
  15. screenshotCamera.Render();
  16. RenderTexture.active = renderTex;
  17.  
  18. RenderTexture current_tex;
  19.  
  20. // Saving by one slice each frame
  21. screenshot.ReadPixels(new Rect(0, 0, width / 4, height), 0, 0);
  22. yield return null;
  23.  
  24. for (int i = 1; i < 4; i++)
  25. {
  26. current_tex = RenderTexture.active;
  27. RenderTexture.active = renderTex;
  28. screenshot.ReadPixels(new Rect(width * ((float)i / 4), 0, width * ((float)i / 4), height), (int) (width * ((float)i / 4)), 0);
  29. RenderTexture.active = current_tex;
  30. screenshotCamera.targetTexture = null;
  31.  
  32. yield return null;
  33. }
  34.  
  35. screenshot.Apply(false);
  36. screenshotCamera.targetTexture = null;
  37.  
  38. RenderTexture.active = null;
  39. Destroy(renderTex);
  40.  
  41. yield return 0;
  42.  
  43. if (screenshot != null && saveToFileName != null)
  44. {
  45. if (Application.platform == RuntimePlatform.OSXPlayer ||
  46. Application.platform == RuntimePlatform.WindowsPlayer &&
  47. Application.platform != RuntimePlatform.LinuxPlayer
  48. || Application.isEditor)
  49. {
  50. byte[] bytes;
  51. if (saveToFileName.ToLower().EndsWith(".jpg"))
  52. bytes = screenshot.EncodeToJPG();
  53. else bytes = screenshot.EncodeToPNG();
  54. FileStream fs = new FileStream(saveToFileName, FileMode.OpenOrCreate);
  55. BinaryWriter w = new BinaryWriter(fs);
  56. w.Write(bytes);
  57. w.Close();
  58. fs.Close();
  59. }
  60. }
  61.  
  62. yield return screenshot;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement