- Home /
Get texture coordinates of mouse when clicking on GUITexture
Can anyone help me with getting the texture coordinates when clicking a GUITexture ?
if tried the following raycast method with no succes on a GUITexture
void OnMouseUp() { // Only when interactive is enabled if (!interactive) return;
RaycastHit hit;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
{
int x = /*width -*/ (int)(hit.textureCoord.x * width);
int y = height - (int)(hit.textureCoord.y * height);
}
}
I'm currently working on in-game browser rendered on a GUITexture. I got some working (See github repo) but I need some mouse support!
Answer by Bampf · Nov 09, 2010 at 12:39 PM
Raycast only hits when there is a Collider component on the object.
According to the documentation, it has to be a mesh collider as well.
Typically this isn't used with GUITextures, but with 3d models. GUITextures aren't in the scene but rather, they float above it facing the camera. So I don't know of a good way to size a mesh collider for it properly without getting into some serious math.
Happily there is an easier way. Since a GUITexture is rectangular, all you need to know are the on-screen dimensions. Then you can compute what part of the image the mouse is over. Call GetScreenRect to get the screen rectangle of the GUITexture. Use it to convert the mouse's screen coordinates into the texture coordinates.
Your answer
Follow this Question
Related Questions
[Solved]Cursor emulation with canvas? 0 Answers
(C#) 2D Raycast wrong direction 0 Answers
How to prevent GameObject from spawning on top of each other? 1 Answer
Drag Object Via Axis Handles 0 Answers
Raycast with strange Behaviour 0 Answers