Unity3d 如何获取鼠标指针下当前像素的颜色

Unity3d 如何获取鼠标指针下当前像素的颜色,unity3d,unity3d-2dtools,unity5,Unity3d,Unity3d 2dtools,Unity5,我想得到当前鼠标指针下像素的颜色 我已经提出了这段代码,但这并没有给出确切的位置,因为Texture2d.GetPixel不适用于float。 这段代码确实给出了颜色,但没有给出精确鼠标位置的颜色,因为我必须将值转换为整数,因为Texture2D.GetPixel无法处理浮点 Texture2D texture; public Color ColorBelowMouse; public Vector3 x; // Use this for initialization void Start (

我想得到当前鼠标指针下像素的颜色

我已经提出了这段代码,但这并没有给出确切的位置,因为Texture2d.GetPixel不适用于float。 这段代码确实给出了颜色,但没有给出精确鼠标位置的颜色,因为我必须将值转换为整数,因为Texture2D.GetPixel无法处理浮点

Texture2D texture;
public Color ColorBelowMouse;
public Vector3 x;

// Use this for initialization
void Start () 
{
    texture=gameObject.GetComponent<GUITexture>().texture as Texture2D;

}

// Update is called once per frame
void Update () 
{
    Debug.Log(texture.GetPixel((int) Input.mousePosition.x, (int) Input.mousePosition.y));
    ColorBelowMouse=texture.GetPixel( (int) Input.mousePosition.x, (int) Input.mousePosition.y);
}
Texture2D纹理;
公共彩色房屋;
公共向量3x;
//用于初始化
无效开始()
{
纹理=gameObject.GetComponent()。纹理为Texture2D;
}
//每帧调用一次更新
无效更新()
{
Log(texture.GetPixel((int)Input.mousePosition.x,(int)Input.mousePosition.y));
ColorBelowMouse=texture.GetPixel((int)Input.mousePosition.x,(int)Input.mousePosition.y);
}
请告诉我如何获得鼠标准确位置的颜色


如果我的方法不对,请告诉我正确的方法。

这似乎有效

Vector2 pos = Input.mousePosition; 
Camera _cam = Camera.mainCamera; 
Ray ray = _cam.ScreenPointToRay(pos);
Physics.Raycast(_cam.transform.position, ray.direction, out hit, 10000.0f);
Color c;
if(hit.collider) {
    Texture2D tex = (Texture2D)hit.collider.gameObject.renderer.material.mainTexture; // Get texture of object under mouse pointer
    c = tex.GetPixelBilinear(hit.textureCoord2.x, hit.textureCoord2.y); // Get color from texture
}
public Texture2D ColorPalleteImage;  //Any Texture Image
public Color ColorBelowMousePointer;
public Rect ColorPanelWidthAndHeight; // set width and height appropriately

void OnGUI() 
{
    GUI.DrawTexture(ColorPanelWidthAndHeight, ColorPalleteImage);

    if (GUI.RepeatButton(ColorPanelWidthAndHeight, ColorPalleteImage))
    {
        Vector2 pickpos  = Event.current.mousePosition;

        float aaa  = pickpos.x - ColorPanelWidthAndHeight.x;

        float bbb  =  pickpos.y - ColorPanelWidthAndHeight.y;

        int aaa2  = (int)(aaa * (ColorPalleteImage.width / (ColorPanelWidthAndHeight.width + 0.0f)));

        int bbb2  =  (int)((ColorPanelWidthAndHeight.height - bbb) * (ColorPalleteImage.height / (ColorPanelWidthAndHeight.height + 0.0f)));

        Color col  = ColorPalleteImage.GetPixel(aaa2, bbb2);

        ColorBelowMousePointer= col;
    }
}

我试过了,但什么也没发生。我无法得到任何颜色。