(Unity) 获取鼠标位置的RGB?

(Unity) Get RGB of mouse position?

HS同学想知道在Unity中如何获取鼠标所在位置的RGB值?我环顾四周,要么是简单的方法不起作用,要么是没有答案,要么是极其复杂的方法,与我正在尝试做的事情无关。

基本上我跟踪了鼠标的位置,现在正在尝试获取其下方图像的RGB值,这不起作用?

这是我的代码,我尝试了 2 种不同的方法,我认为这些方法可行但行不通(我知道我不需要 Raw Image 和 Texture2D,这只是每种方法都尝试过的方法)。

public Text cords = null;
int mousex;
int mousey;

public RawImage pic;
public Text textColor = null;
public Texture2D image;

void Start () {

}


void Update () {
    mousex = (int)Input.mousePosition.x;
    mousey = (int)Input.mousePosition.y;
    cords.text = "X: " + mousex + "   Y: " + mousey;

    //Color32 color = (pic.texture as Texture2D).GetPixel(mousex, mousey);
    Color32 color = image.GetPixel(mousex, mousey);
    //textColor.text = color.ToString();
    Color32 Hovercolor;
    if (Input.GetMouseButtonDown(0)) {
        Hovercolor = image.GetPixel(mousex, mousey);
        textColor.text = Hovercolor.ToString();
    }


}

要使用 Texture2D.GetPixel 从 texture.Note 获取像素:

The texture must have the read/write enabled flag set in the texture import settings, otherwise GetPixel will fail. GetPixel is not available on Textures using Crunch texture compression.

而且我发现你使用Input.mousePosition作为索引在texture.You中寻找像素也应该注意,一旦它们不匹配:

If the pixel coordinates are out of bounds (larger than width/height or small than 0), they will be clamped or repeated based on the texture's wrap mode.