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.
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.
Here is the function:
public Vector3 WorldToGuiPoint(Vector3 position)
{
var guiPosition = Camera.main.WorldToScreenPoint(position);
guiPosition.y = Screen.height - guiPosition.y;
return guiPosition;
}
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);
}
This functionality is built into Unity. You need GUIUtility.ScreenToGUIPoint
.
Check the unity scripting API for usage: check this out
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.