如何使用Unity和C#从鼠标位置的屏幕捕获颜色? [英] How To Capture the color from screen in mouse position using Unity and C#?
本文介绍了如何使用Unity和C#从鼠标位置的屏幕捕获颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在颜色"部分找不到直接函数,或者因为没有直接的C#Unity函数从后期渲染中拾取颜色.在鼠标位置选择颜色的最佳方法是什么?
我已经进行了研究,看起来可以制作一个屏幕截图,然后查看纹理以计算鼠标位置.
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?
推荐答案
您只需要使用GetPixel(x,y)
这很简单.
- 将截图保存到Texture2D中,例如MyTexture
- 并添加.GetPixel(x位置的Moue,鼠标的Y位置)
-
将其保存为GetScreenShot的颜色(使您的视图成为Texture2D)
- 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);
}
这篇关于如何使用Unity和C#从鼠标位置的屏幕捕获颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文