2015-09-21 5 views
0

Я хочу получить цвет пикселя, который в настоящее время находится под указателем мыши.Как получить цвет пикселя в данный момент под указателем мыши

У меня есть этот код, но это не дает точного положения, поскольку 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); 
} 

Пожалуйста, скажите мне, как получить цвет точную позицию мыши.

Если мой подход неправильный, пожалуйста, сообщите мне правильный.

ответ

0
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 
} 
+0

Я попытался, но ничего не произошло. я не мог получить никакого цвета. –

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