0

現在マウスポインターの下にあるピクセルの色を取得したい。

私はこのコードを思いつきましたが、Texture2d.GetPixel が浮動小数点数で機能しないため、これは正確な位置を示しません。このコードは色を提供しますが、Texture2D.GetPixel は float を処理できないため、値を整数にキャストする必要があるため、正確なマウス位置の色は提供しません。

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);
}

正確なマウス位置の色を取得する方法を教えてください。

私のアプローチが間違っている場合は、正しいアプローチを教えてください。

4

2 に答える 2

0

これは機能しているようです!

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;
    }
}
于 2015-09-21T10:20:30.253 に答える