- Home /
Take screenshot and use it as texture using ScreenCapture
I need to take screenshot and hold it in memory so later I can serialise it in save file with all the other information I need to save. We're using Unity 2019.3.0a8 and as documentation says for taking screenshots there is class ScreenCapture for which I wasn't able to find any information except for Unity Scripting API. So I have two questions.
First, as documentation says I need to wait till the end of frame rendering and the proper way of doing so is (taken from ScreenCapture.CaptureScreenshotAsTexture)
IEnumerator TakeScreenshot() { yield return new WaitForEndOfFrame(); var texture = ScreenCapture.CaptureScreenshotAsTexture(); // do something with texture // cleanup Object.Destroy(texture); }
However if I'm using yield return, I can't have neither return value for the method nor ref parameter thus I can't use the screenshot inside the method in which I call StartCoroutine(TakeScreenshot());
. I was thinking of using async/await but it allows me to wait only for some amount of seconds and not exactly till the end of frame. How can I wait for end of frame rendering without using yield?
Second question here.
Answer by kala-hari · Jul 17, 2019 at 06:53 PM
One way to solve the issue with returning values from Coroutines is passing a Callback to the Coroutine method.
The Callback can be called after the screenshot is taken, and receive the result as a Parameter.
For example:
void HandleScreenshot()
{
StartCoroutine(TakeScreenshot((result) => {
FurtherProcessing(result);
});
}
IEnumerator TakeScreenShot(System.Action<ResultType> callback)
{
yield return new WaitForEndOfFrame();
var result = new ResultType(ScreenCapture.CaptureScreenshotAsTexture());
callback(result);
}
Don't forget to destroy the texture aftwards.
I've just added all further processing into TakeScreenshot. I just don't know why didn't I do this in first place. Anyway, thank you for the help.
Answer by WookieWookie · May 06 at 08:49 PM
I get this error that is a known bug but only has 2 upvotes:
Metal: GrabIntoRenderTexture mismatched grab pass: 80 / 70