Hi,
I’m having huge problems getting to take a screenshot of a particular part of the screen. I’ve tried several things but none of them seem to work. I’m using NGUI to build my GUI and the part of the screen i’m trying to take a screenshot off is part of a UITexture with several other textures.
The screenshot function is working but it always starts at the wrong coordinates.
Here’s my code:
banknote.worldCorners[0] is the bottomLeft point of the texture
banknote.worldCorners[1] is the topLeft point of the texture
public class TakeScreenShot : MonoBehaviour
{
//The banknote UITexture
public UITexture banknote;
//Camera used by NGUI
public UICamera cam;
//The rect with the size and coordinates of the banknote
private Rect texRect;
private IEnumerator ScreenShot()
{
Debug.Log ("Grabbing texture!");
Texture2D tex = new Texture2D((int) texRect.width, (int) texRect.height, TextureFormat.RGB24, false);
yield return new WaitForEndOfFrame();
tex.ReadPixels(texRect, 0, 0);
tex.Apply();
Destroy(tex);
byte[] bytes = tex.EncodeToPNG();
File.WriteAllBytes(Application.persistentDataPath + "/Banknote.png", bytes);
StartCoroutine(ScreenshotManager.SaveExisting( bytes, "banknote"));
}
public void CaptureImage()
{
//Set the right coordinates for the picture
Vector3 NoteBottomRight = cam.camera.WorldToScreenPoint(banknote.worldCorners[3]);
Vector3 NoteTopLeft = cam.camera.WorldToScreenPoint(banknote.worldCorners[1]);
int width = (int) (NoteBottomRight.x - NoteTopLeft.x);
int height = (int) (NoteTopLeft.y - NoteBottomRight.y);
float x = banknote.worldCorners[0].x; //Left side of the banknote
float y = banknote.worldCorners[1].y; //Top Y of the banknote
Debug.Log ("Starting capturing image:\n x: " + x + "\n y: " + y + "\n width: " + width + "\n height: " + height);
texRect = new Rect(x, y, width, height);
Debug.Log (texRect);
StartCoroutine("ScreenShot");
}
}
I’m trying to take a screenshot of the following item (whole banknote):
And the result:
As you can see the size is about right, but the pixel coordinates are way off. Any idea how to fix this?
I forgot to mention, i’ve created some simple primitives (cubes) to check if the positions are right. And when I create a primitive at banknote.worldCorners[0] it is positioned at the bottom left corner of the banknote. Same goes for banknote.worldCorners[1] the cube is then positioned at the top left corner of the banknote.