- Home /
Using Screenshot as Texture
I want to take a screenshot and use it as a material texture for another object.
Script so far:
public IEnumerator TakeSnapshot(int width, int height)
{
yield return new WaitForSeconds(5);
Texture2D texture = new Texture2D (width, height, TextureFormat.RGB24, true);
texture.ReadPixels(new Rect(0, 0, width, height), 0, 0);
snapshot = texture; }
then im using
fotoPlane.renderer.material.mainTexture = snapshot;
but this is just creating a gray texture instead of getting the screen image.
Answer by nogue2k · Jul 04, 2012 at 03:28 PM
i forgot the texture.Apply(); after texture.ReadPixels. That solved it. I will leave the question here in case someone has the same problem.
StartCoroutine(TakeSnapshot(Screen.width,Screen.height));
public IEnumerator TakeSnapshot(int width, int height) { yield return new WaitForSeconds(0.1F); Texture2D texture = new Texture2D (width, height, TextureFormat.RGB24, true); texture.ReadPixels(new Rect(0, 0, width, height), 0, 0); texture.Apply(); snapshot = texture; }
and then
gameObject.renderer.material.mainTexture = snapshot;
Hey this worked great for me, but I am getting an error ReadPixels was called to read pixels from system frame buffer, while not inside drawing frame. Any way to fix this?
Answer by moonchacha · Mar 07, 2014 at 08:39 PM
Nice. It looks like using "yield return new WaitForEndOfFrame();" might be a little cleaner.
Answer by Raclette · Jun 03, 2017 at 11:33 AM
Thank you it’s very useful for me. I was wondering if it’s possible to snapshot a specific region of the screen? I try to make a little drawing game and I want to save only the region of the paper. Any idea how can I do that? Thank you