using System.Collections; using System.Collections.Generic; using UnityEngine; public class ScreenshotHandler : MonoBehaviour { private static ScreenshotHandler instance; private Camera myCamera; private bool takeScreenshotOnNextFrame; public string SavePath; private void Awake() { instance = this; myCamera = GetComponent(); } private void OnPostRender() { if (takeScreenshotOnNextFrame) { takeScreenshotOnNextFrame = false; RenderTexture renderTexture = myCamera.targetTexture; Texture2D renderResult = new Texture2D(renderTexture.width, renderTexture.height, TextureFormat.ARGB32, false); Rect rect = new Rect(0, 0, renderTexture.width, renderTexture.height); renderResult.ReadPixels(rect, 0, 0); byte[] byteArray = renderResult.EncodeToPNG(); string file = SavePath; Debug.Log(file); System.IO.File.WriteAllBytes(file, byteArray); RenderTexture.ReleaseTemporary(renderTexture); myCamera.targetTexture = null; } } private void TakeScreenshot(int widht, int height) { myCamera.targetTexture = RenderTexture.GetTemporary(widht, height, 16); takeScreenshotOnNextFrame = true; } public static void TakeScreenshot_static() { instance.TakeScreenshot(Screen.width, Screen.height); } public static void SetPath(string Path) { instance.SavePath = Path; } }