unity3d - How to get the color of pixel currently under mouse pointer -
i want color of pixel under mouse pointer.
i have come code, not give exact position texture2d.getpixel not work float. code give color not give color of exact mouse position have cast values integer since texture2d.getpixel cant handle float
texture2d texture; public color colorbelowmouse; public vector3 x; // use initialization void start () { texture=gameobject.getcomponent<guitexture>().texture texture2d; } // update 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); }
please tell me how color of exact mouse position.
if approach wrong, please tell me correct one.
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; // texture of object under mouse pointer c = tex.getpixelbilinear(hit.texturecoord2.x, hit.texturecoord2.y); // color texture }
Comments
Post a Comment