I\'ve been trying to take a screenshot and then immediately after, use it to show some sort of preview and some times it works and some times it doesn\'t, I\'m currently not
Programmer's code worked successfully by being called like the following. It is designed as coroutine, so it would not interfere frame rate. Hence it should be called as coroutine. Make sure the CallerObject is inheriting from "MonoBehaviour".
public class CallerObject : MonoBehaviour
{
public void Caller()
{
String imagePath = Application.persistentDataPath + "/image.png";
StartCoroutine(captureScreenshot(imagePath));
}
IEnumerator captureScreenshot(String imagePath)
{
yield return new WaitForEndOfFrame();
//about to save an image capture
Texture2D screenImage = new Texture2D(Screen.width, Screen.height);
//Get Image from screen
screenImage.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
screenImage.Apply();
Debug.Log(" screenImage.width" + screenImage.width + " texelSize" + screenImage.texelSize);
//Convert to png
byte[] imageBytes = screenImage.EncodeToPNG();
Debug.Log("imagesBytes=" + imageBytes.Length);
//Save image to file
System.IO.File.WriteAllBytes(imagePath, imageBytes);
}
}