The question is answered, right answer was accepted
Show screen capture as UI image
Hello, I'm trying to take a picture of the screen and assign it as a sprite for an UI image.
However, for some reason, all I get is a white sprite.
 public Camera renderCam;
 int photoWidth = 100, photoHeight = 100;
 
 public void LateUpdate()
     {
         if (snapped)
         {
             RenderTexture rt = new RenderTexture(photoWidth, photoHeight, 24);
             renderCam.targetTexture = rt;
             RenderTexture.active = rt;
             renderCam.Render();
             Texture2D screenShot = new Texture2D(photoWidth, photoHeight, TextureFormat.RGB24, false);
             screenShot.ReadPixels(new Rect(0, 0, photoWidth, photoHeight), 0, 0);
             renderCam.targetTexture = null;
             GameObject flyToHud = GameObject.Instantiate(photoToHudAnimation);    
             flyToHud.GetComponent<Image>().sprite = Sprite.Create(screenShot, new Rect(0, 0, photoWidth, photoHeight), new Vector2(0, 0));
 
             Camera.main.targetTexture = null;
             RenderTexture.active = null; // JC: added to avoid errors
             Destroy(rt);
             snapped = false;
         }
     }
 
               I'm pretty sure the problem is with Sprite.Create(), for 2 reasons:
1) I tried saving the RenderTexture rt to disk as PNG right before the Sprite.Create() method and it worked, so it is indeed storing a capture of the screen.
2) I also already tried simply assigning a public sprite otherSprite to the instantiated object like this:
 flyToHud.GetComponent<Image>().sprite = otherSprite;
 
               and the proper sprite was shown. So I'm sure there is no other part of the code replacing the sprite with a white rectangle.
Answer by Chambers88 · Feb 25, 2018 at 06:22 PM
I realised it was not a pure white sprite, but a very bright grey (205) sprite, which allowed me to find similar questions.
Just fixed it by calling screenShot.Apply() before creating the sprite.
Thank you for this, I used it to make a cool transition :)
Follow this Question
Related Questions
Resource.Load always returning null when trying to load an image 1 Answer
HOW TO GET AN APPEARANCE OF A SCENE BY SCRIPT?????????? 0 Answers
Can I use unity UI image in the ARKIT ? 0 Answers
Object reference not set to instance of an object? 1 Answer
How to check if two sprites are the same 2 Answers