- Home /
[CLOSED]How to take a screenshot and apply it as a texture to a sprite?
So what im trying to do is take a render from the screen, turn it into a Texture2D and apply it to a sprite using Sprite.Create();
Here is my code:
// instance of the camera im getting on Awake()
private Camera _camera;
private Texture2D _screenShot;
private IEnumerator TakeScreenShot()
{
yield return new WaitForEndOfFrame();
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
_camera.targetTexture = rt;
_screenShot= new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
_camera.Render();
RenderTexture.active = rt;
_screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
_camera.targetTexture = null;
RenderTexture.active = null;
Destroy(rt);
string filename = ScreenShotName(resWidth, resHeight);
//byte[] bytes = _screenShot.EncodeToPNG();
//System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("Took screenshot to: {0}", filename));
Sprite tempSprite = Sprite.Create(_screenShot,new Rect(0,0,resWidth,resHeight),new Vector2(0,0));
GameObject.Find("SpriteObject").GetComponent<SpriteRenderer>().sprite = tempSprite;
}
All it does is change the SpriteObject.sprite color to white. But when I try to save the content of the screenshot it turns out that there is actualy an image and not just white.
I know it's an old post, but you saved my life with your code mate, so just wanted to say thank you sooooooo much !
What is the ScreenShotName, it says it doesn't exist, am I missing something?
ScreenShotName is a method directly made by brunopova, you don't really need it. Here's my version of the code if you're interested :
private IEnumerator OnPause()
{
yield return new WaitForEndOfFrame();
//Create a new Texture2D of the Height and width of the screen, before reading all the pixels and
//put them in the ney Texture2D
texture = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, true);
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0, true);
texture.Apply();
//Create a bytes array containing the texture2D
byte[] b = texture.EncodeToPNG();
//Path + name of the screenshot
string date = System.DateTime.Now.ToLongTimeString();
screenShotPath = Application.persistentDataPath + "/ScreenShot_" + date + ".png";
//save the screenshot
File.WriteAllBytes(screenShotPath, b);
texture.LoadImage(b);
//create a new sprite with the texture2D
Sprite sprite = Sprite.Create(texture,
new Rect(0.0f, 0.0f, texture.width, texture.height),
new Vector2(0.5f, 0.5f),
100.0f);
//Put the new created sprite inside a panel
Comment$$anonymous$$odeBackground.GetComponent<UnityEngine.UI.Image>().sprite = sprite;
Comment$$anonymous$$odeBackground.GetComponent<UIController>().Show();
RemoveAndAddElementFromScreen(true);
}
The last line is another method I created to disable all UI element of screen to avoid having them when we make the screenshot.
Thanks, man. I made it, but this is not what I searched for =(. This code makes rectangular screenshots, what I searched is can be any shape.
Oh ok, so maybe should you add before calling this method another function which create a mask with the shape that you want ?
Answer by PAEvenson · Jun 23, 2014 at 01:16 PM
Perhaps you are missing apply after you read the pixels?
_screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
_screenShot.Apply(); //Add this?
_camera.targetTexture = null;
Your answer
Follow this Question
Related Questions
Using Texture2D.GetPixels() to take a screenshot and then show it on an Image - iOS problems. 1 Answer
Struggling to find how to access the Texture 2D of a sprite. 1 Answer
Coding my own auto-slicer, getting "islands" of pixels at runtime? 1 Answer
Convert a Texture2D to Sprite 2 Answers
Show Screenshot image on GUI, linear color space , Need help 1 Answer