Read a part of the screen with AsyncGPUReadback

Hello all ! I’d like to read the color under the cursor of the mouse in game. ReadPixels is not acceptable because it’s way too slow.
I stumbled upon this class, AsyncGPUReadback, which could fit my needs.
The idea is to take a screenshot, then get the mouseposition, put in a texture the pixel under the mouse,
and use the Request method to read it.
Here’s the code I have right now :

			// define a texture of the size of the screen
			var renderTexture = RenderTexture.GetTemporary(Screen.width, Screen.height, 32);

			format = renderTexture.graphicsFormat;

			// makes a screenshot and put it into the texture
			ScreenCapture.CaptureScreenshotIntoRenderTexture(renderTexture);

			// read the texture and do something with it in OnCompleteReadback
			AsyncGPUReadback.Request(renderTexture, 0, TextureFormat.RGBA32, OnCompleteReadback);

And here the code in OnCompleteReadback :

	void OnCompleteReadback(AsyncGPUReadbackRequest request)
	{
        int currentX = (int) Input.mousePosition.x;
        int currentY = (int) Input.mousePosition.y;
		byte[] array = request.GetData<byte>().ToArray();
		Color point = new Color();
       //do something with the color ....
	}

There’s two things here. The first one is that with this array of byte, I’m not sure how to get the color under the cursor. Knowing where my cursor is, how do I get the bytes I need ?
The second thing is that I’m taking a screenshot of the whole screen, but since I know precisely which pixel I want, isn’t there a way to get a capture of this unique pixel, instead of a whole picture ?

Hi @kasmurdanto,

I’m incredbily late to this thread, but I managed to do it. Here’s the code:

public void GetPixelAsync(int x, int y)
    {
        AsyncGPUReadback.Request(renderTexture, 0, xCoord, 1, yCoord, 1, 0, 1,
            request =>
            {
                if (request.hasError) return;

                Color32 requestedColor = request.GetData<Color32>(0)[0];

                print(requestedColor);
            }
        );
    }

Hope this helps anyone who has the same problem :slight_smile: