Search code examples
unityscriptunity-game-engine

How To Capture the color from screen in mouse position using Unity and C#?


I did not find a direct function in Color section or since there is no direct C# Unity function for picking the color from post render. How is the best approach for picking the color in the mouse position?
I have done research and looks like there is posible to make a screenshot and then look into the texture calculating the mouse position.

Input.GetMouseButtonDown(0)

Application.CaptureScreenshot("Screenshot.png");

// get the color pixel in the same coordinates of the mouse position
Vector3 mouseCoordinates = Input.mousePosition;
myFinalColor = tex.GetPixel((int)mouseCoordinates.x, (int)mouseCoordinates.y);

Or do I have to make a second camera and attach it to a mesh render?


Solution

  • You just need to use GetPixel(x,y)

    This is very simple.

    • Save Screenshot to Texture2D for example MyTexture
    • And add .GetPixel( x postion of moue , Y position of mouse )
    • Save it to your color for GetScreenShot ( make your View to Texture2D )

          Color TheColorPicked;
          if (Input.GetMouseButtonDown(0))
          {
              TheColorPicked = MyTexture.GetPixel(Input.mousePosition.x,
                                                     Input.mousePosition.y);    
          }