So I am just using ScreenCapture.CaptureScreenshotAsTexture() and it pops out a milky white texture. Below I put the screenshot on top of the source, then again and again and again. And as you see, each time a screenshot is taken, it makes it milky, and again and again…
I am trying to take a screenshot of pure UI.
Anybody know what is the problem? I am on 2019.2.0f1. Attached is the code that generates.
public class ScreenshotTaker : MonoBehaviour
{
public Image target;
IEnumerator RecordFrame()
{
while( true )
{
Debug.Log( "Click" );
yield return new WaitForEndOfFrame();
Texture2D texture = ScreenCapture.CaptureScreenshotAsTexture();
// do something with texture
target.sprite = Sprite.Create( texture, new Rect( 0.0f, 0.0f, texture.width, texture.height ), new Vector2( 0.5f, 0.5f ), 100.0f );
yield return new WaitForSeconds( 2 );
}
}
public void Awake()
{
StartCoroutine(RecordFrame());
}
}
Just ran into your exact same problem. Using Unity 2019.3.0f1. There’s probably something buggy with the ScreenCapture.CaptureScreenshotAsTexture(). I solved it using this code instead:
int width = Screen.width;
int height = Screen.height;
Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
tex.ReadPixels(new Rect(0, 0, width, height), 0, 0);
tex.Apply();
Since Unity hasn’t fixed the issue yet, I ended up copying the texture created by ScreenCapture.CaptureScreenshotAsTexture() into a new texture and using that.
private Texture2D TakeScreenShot()
{
Texture2D screenShot = ScreenCapture.CaptureScreenshotAsTexture();
// All the following is necessary due to a Unity bug when working in Linear color space:
Texture2D newScreenShot = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
newScreenShot.SetPixels(screenShot.GetPixels());
newScreenShot.Apply();
return newScreenShot;
}