Sprite is too bright after Sprite.Create()

Hi everybody.
I’m trying to make a screenshot of the game, turn it into a sprite and use it to sort of fool the player into thinking where he currently is while actually moving him to another place.
So basically my code looks like this:

Sprite usedSprite;
Image fadeInImage;
Texture2D Tex2D;
private IEnumerator FadeToScreenshotSetupRoutine()
{
   yield return new WaitForEndOfFrame();
   usedSprite = MakeNewSprite(ScreenCapture.CaptureScreenshotAsTexture());
   fadeInImage.sprite = usedSprite;
   StartCoroutine(FadeInImageRoutine()); //starts changing the alpha of fadeInImage from 0 to 1
}

public Sprite MakeNewSprite(Texture2D _tex2D)
{
   Tex2D = _tex2D;
   GaussianBlur(ref Tex2D); //blur the image
   return Sprite.Create(Tex2D, new Rect(0, 0, Tex2D.width, Tex2D.height), new Vector2(0.5f, 0.5f));
}

The issue here is that the resulting sprite is way too bright and it’s colors seems to be a bit off.
If I save the Tex2D after applying a gausian blur as a PNG - it looks exactly as it should look. I can even import that PNG file into the assets folder later, change it’s type in the inspector to Sprite and use it in the game and it looks good. So I’m assuming something is going wrong at the step when I do Sprite.Create().
I’m not 100% sure, but it seems like the freshly created sprite has a property (from the inspector) “sRGB (Color Texture)” toggled off by default, since it’s exactly how the image looks, if toogle this flag off on a saved and imported into Unity PNG file.
It seems like the sRGB flag is not present on the created texture even at the stage when I’m making a screenshot, since I’ve tried to change the Image to RawImage (to avoid conversion to Sprite), but the result is still the same

Is there a way to toggle this property on a sprite freshly made Texture2D using code? Or maybe I’m doing something wrong?
Thanks.

Try something like this:

private Sprite CreateScreenshotSprite(bool blur = true)
{
    var texture = new Texture2D(Screen.width, Screen.height);

    var rect = new Rect(0, 0, Screen.width, Screen.height);

    texture.ReadPixels(rect, 0, 0);

    texture.Apply();

    if (blur)
        ApplyGaussianBlur(texture);

    return Sprite.Create(texture, rect, Vector2.zero);
}
1 Like

Tried the code, and it works correctly (no Blur)

Thank you! ReadPixels really did help!

1 Like