You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.5 KiB
53 lines
1.5 KiB
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<Camera>(); |
|
} |
|
|
|
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; |
|
} |
|
} |