- Home /
ScreenToGUIPoint
Could somebody explain to me how I can convert a screenPoint to a GUIPoint. I've tried the GUIUtility.ScreenToGUIPoint but it doesn't seem to return what I want. Heres the testing code. The guiPoint remains the same as the screenPoint for some reason.
Vector3 viewportPoint = new Vector3(1,1,1);
Vector3 screenPoint = new Vector3(1,1,1);
Vector2 guiPoint = new Vector2(0, 0);
void OnGUI()
{
guiPoint = GUIUtility.ScreenToGUIPoint(new Vector2(screenPoint.x, screenPoint.y));
GUILayout.BeginArea(new Rect(0, 0, 200, 200));
GUILayout.Label("Viewport X: " + viewportPoint.x);
GUILayout.Label("Viewport Y: " + viewportPoint.y);
GUILayout.Label("Screen X: " + screenPoint.x);
GUILayout.Label("Screen Y: " + screenPoint.y);
GUILayout.Label("Gui X: " + guiPoint.x);
GUILayout.Label("Gui Y: " + guiPoint.y);
GUILayout.EndArea();
}
void OnMouseOver()
{
viewportPoint = transform.parent.camera.ScreenToViewportPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,0));
screenPoint = transform.parent.camera.ViewportToScreenPoint(viewportPoint);
}
}
Answer by Eric5h5 · Jul 11, 2011 at 04:59 PM
guiPoint = screenPoint;
guiPoint.y = Screen.height-guiPoint.y;
I had the same problem. Does ScreenToGUIPoint just not work?
@ The$$anonymous$$aster42: I used it in my code to get the position of the mouse on the screen. Here is the link to question which contains this code. http://answers.unity3d.com/questions/457737/mouse-screen-position-and-gui-position-not-scaled.html
I haven't tested this myself, but this appears to be caused by the the classic DX vs OpenGL pixel-read order. If you're rendering w/ DX10/11/12/etc. I think pixels are read from top to bottom, whereas if you're rendering in OpenGL, they're read from bottom to top. If this inversion is necessary still, it would prob be due to GUIUtility assu$$anonymous$$g one one render path while you're using the other. Not sure which, but if anyone else runs into this, try switching from DX to OpenGLor vice versa to see if that fixes it.
Your answer
Follow this Question
Related Questions
how to calculate screen to world point (custom) 0 Answers
Interface 3D 1 Answer
full screen wide GUI.Box 3 Answers
know GUILayout current screen position 2 Answers
GUI button in middle of screen 2 Answers