- Home /
 
Convert world space to GUI Rect?
Hi, I'd like to render a GUI texture at a GameObject's position, is that possible?
What I need is just a way to convert the world space of my GameObject, into a Vector2 (or Rect).
Thanks, Andreas.
Answer by Professor Snake · Oct 13, 2013 at 06:15 PM
You can use Camera.WorldToScreenPoint().
Answer by Vacummus · Jun 15, 2014 at 04:31 PM
Here is the function:
 public Vector3 WorldToGuiPoint(Vector3 position)
 {
     var guiPosition = Camera.main.WorldToScreenPoint(position);
     guiPosition.y = Screen.height - guiPosition.y;
 
     return guiPosition;
 }
 
              Answer by Cherno · Oct 13, 2013 at 08:54 PM
 var WorldNamePos : Vector3;
 var ObjectTexture : Texture2D;
 
 function OnGUI()
 {
     WorldNamePos = Camera.main.camera.WorldToScreenPoint(Vector3(transform.position.x, transform.position.y + 0.5, transform.position.z));
     GUI.DrawTexture (Rect (WorldNamePos.x, Screen.height - WorldNamePos.y - 10, ObjectTexture.width, ObjectTexture.height), ObjectTexture);
 }
 
 
 
              4 years later and still useful answer thank you @Cherno!
Glad to be of help, good sir. Happy coding!
Answer by VSZM · Dec 28, 2014 at 10:23 PM
This functionality is built into Unity. You need GUIUtility.ScreenToGUIPoint.
Check the unity scripting API for usage: check this out
Answer by phoenixperry1 · Mar 30, 2014 at 11:21 PM
Nope. I don't think you can do this with stuff generated in OnGui because that stuff uses a different screen space than WorldToScreenPoint. You're going to need to flip the Y axis for that solution to work.
Your answer
 
             Follow this Question
Related Questions
GUI in 3D world space "Monitors / Screens / Displays" 1 Answer
Can't destroy an ui element 2 Answers
Rect to RectTransform on overlay Canvas? 1 Answer
Help with destroying guiRect? 0 Answers