- Home /
GUI Button and a gameObject's position
Is there an easy way to have a GUI Button show up over a game object? I'm aiming for various items on a desk that have writing on them saying what will happen if the user clicks on them. (E.g. keys for "Shop", paper for "Write".) I was hoping this would be simpler than making the objects clickable and just positioning GUITextures over them.
Answer by Statement · Mar 01, 2011 at 03:47 PM
You can ask the camera where any point in world space will be in screen space.
Try this to get a feeling (untested code, might contain errors):
public class Example : MonoBehavior { // Bind target to the object it should follow. public Transform target;
// We use update to obtain the position in this
// example so we need to keep it until OnGUI.
Vector3 point;
void Update()
{
// Find screen position for target
point = Camera.main.WorldToScreenPoint(target.position);
}
void OnGUI()
{
// In case point.y is inverted, just subtract it from the screen height.
Rect rect = new Rect(point.x, point.y, 100, 50);
if (GUI.Button(rect, "Button")
{
// Button clicked.
}
}
}
Thank you very much. Although I'm mainly a javascripter, you've definitely put me on the right track. I'll need to do a little more tweaking to get it working just right, but at least I now know what I have to do. Thanks again!
hi, i didnt understand where do i have to subtract the the point.y im nbot sure if im using scree height i have this:
void Update()
{
// Find screen position for target
point = Camera.main.WorldToScreenPoint(target.position);
}
void OnGUI()
{
float yPos = 5.0f;
float xPos = 5.0f;
float width = ( Screen.width >= 960 || Screen.height >= 960 ) ? 100 : 100;
float height = ( Screen.width >= 960 || Screen.height >= 960 ) ? 100 : 100;
float heightPlus = height + 0.0f;
// In case point.y is inverted, just subtract it from the screen height.
Rect rect = new Rect(point.x, point.y, 100, 100);
** I've tried change the screen height that appears in the code but, that is for some other static GUI buttons.
And one more question, how do put a position for example if want that the GUI button follow the object but in a specific position (a square object and the GUI button follow it but always in the corner)
appreciate the help!
Answer by octaviomejiadiaz · Jul 09, 2012 at 06:21 AM
hi, i didnt understand where do i have to subtract the the point.y im nbot sure if im using scree height i have this:
void Update()
{
// Find screen position for target
point = Camera.main.WorldToScreenPoint(target.position);
}
void OnGUI()
{
float yPos = 5.0f;
float xPos = 5.0f;
float width = ( Screen.width >= 960 || Screen.height >= 960 ) ? 100 : 100;
float height = ( Screen.width >= 960 || Screen.height >= 960 ) ? 100 : 100;
float heightPlus = height + 0.0f;
// In case point.y is inverted, just subtract it from the screen height.
Rect rect = new Rect(point.x, point.y, 100, 100);
** I've tried change the screen height that appears in the code but, that is for some other static GUI buttons.
And one more question, how do put a position for example if want that the GUI button follow the object but in a specific position (a square object and the GUI button follow it but always in the corner)
appreciate the help!