ScreenCapture.CaptureScreenshotAsTexture() is making a milky white tinted screenshot.
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());
}
}
Answer by HardK · Dec 10, 2019 at 12:25 PM
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();
Cheers
Answer by angelonit · Nov 02, 2021 at 06:42 PM
I ended up doing this : From here: https://answers.unity.com/questions/1473282/show-screen-capture-as-ui-image.html
public void TakeScreenShot()
{
int photoWidth = Screen.width;
int photoHeight = Screen.height;
RenderTexture rt = new RenderTexture(photoWidth, photoHeight, 24);
GameStateControl.control.cameraPlugin.cam.targetTexture = rt;
RenderTexture.active = rt;
GameStateControl.control.cameraPlugin.cam.Render();
Texture2D screenShot = new Texture2D(photoWidth, photoHeight, TextureFormat.RGB24, false);
screenShot.ReadPixels(new Rect(0, 0, photoWidth, photoHeight), 0, 0);
screenShot.Apply();
GameStateControl.control.cameraPlugin.cam.targetTexture = null;
Image img = GetComponent<Image>();
img.sprite = Sprite.Create(screenShot, new Rect(0, 0, photoWidth, photoHeight), new Vector2(0, 0));
Camera.main.targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
}