Hello everybody! I figured out how to use ReadPixels to capture the full screen, but I completely do not understand how do the coordinates work with it, because I get just some crazy pictures when I pass some parameters like the ones that are in my script. Please help!
The drawContents Image is the UI Image that I want to capture and to attach to itself(I am going to draw over it and apply the changes). This image is a child of another image that has a MASK component. I am not sure that this can be relevant, but telling the details just in case.
My function is:
private void ApplyChanges()
{
Texture2D snapShot = new Texture2D((int)drawContents.rect.width, (int)drawContents.rect.height, TextureFormat.ARGB32, false);
RenderTexture snapShotRT = new RenderTexture((int)drawContents.rect.width, (int)drawContents.rect.height, 24, RenderTextureFormat.ARGB32);
RenderTexture.active = snapShotRT;
lineRenderingCamera.targetTexture = snapShotRT;
lineRenderingCamera.Render();
snapShot.ReadPixels(new Rect(drawContents.rect.position.x, drawContents.rect.position.y, snapShotRT.width, snapShotRT.height), 0, 0);
Debug.Log(new Rect(drawContents.rect.position.x, drawContents.rect.position.y, snapShotRT.width, snapShotRT.height));
snapShot.Apply();
drawContents.GetComponent<Image>().sprite = Sprite.Create(snapShot, new Rect(0, 0, snapShot.width, snapShot.height), new Vector2(0.5f, 0.5f));
RenderTexture.active = null;
lineRenderingCamera.targetTexture = null;
}
what I get as a result:
But I want to capture the part of the screen that is gray. How do I do it? Help, please!
Upd: I also have thought of capturing the full screen and reading specific pixels from the texture that contains the screenshot, but I just can’t find the way to read the specific pixels from one texture and upload them to another.
Upd: My canvas is rendered in mode Screen Space - Camera
Upd: I managed to capture a part of screen with the help of mouseposition, so if I manually click on the lower left corner of my image and on the top right corner it captured the needed space. So I figured that the problem is 100% about the coordinates, and I found that approximate coords of the needed rect in screenspace are 27 134 for lower left and 348 538 for top right. But how do I get the exact coordinates through code?