- Home /
The question is answered, right answer was accepted
Create Sprite from 2DTexture - only returns white-ish field
Hello there,
i aim to render a cameras view, output it as a 2dTexture and to then convert it to a sprite so that i can display it as an image in my ui.
It works if i take the step to save the image and reload it from there as sprite but this is stupid concerning performance. So the approch i wanted to take is to use Sprite.Create()
to directly convert the 2DTexture into a sprite. But this only returns a evenly colored white/grey -ish field.
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
myCamera.enabled = true;
myCamera.targetTexture = rt;
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
myCamera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
Sprite.Create(screenShot, new Rect(0, 0, resWidth, resHeight), new Vector2(0.5f,0.5f),100.0f);
The Texture2D screenShot
contains the right content since the conversion and saving in png format works.
If anyone happens to ahve some insight for me on this i'd be glad to hear it. If you need more information on the project/code feel free to ask. Thanks in advance!
What a great post that I had been seeking to Create Sprite from 2DTexture for a long time, and at last I found the some real information about it, thanks for sharing this type of information and letting us know about it. https://daswortwurdefleisch.de/
Answer by KISP · Jan 30, 2019 at 09:47 PM
You need to make a call to screenshot.Apply() after using ReadPixels().
This question might also help you: https://answers.unity.com/questions/733240/how-to-take-a-screenshot-and-apply-it-as-a-texture.html
Hey there, thanks a lot for the input! Gonna check this when i get home this evening :)
OMG you've really saved my life, i know this is an old post but i must login and show my gratitude to you, wish you the best of luck
Follow this Question
Related Questions
Sprite.Create on iOS crops images 1 Answer
Help filling in a 2dTexture. 0 Answers
How to create sprite at run time? 1 Answer
Create colored rectangle at runtime 1 Answer