x


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.

more ▼

asked Mar 01 '11 at 03:33 PM

Orandze gravatar image

Orandze
15 1 2 6

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.
        }
    }
}
more ▼

answered Mar 01 '11 at 03:47 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

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!

Mar 01 '11 at 04:38 PM Orandze

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!

Jul 09 '12 at 02:43 AM octaviomejiadiaz
(comments are locked)
10|3000 characters needed characters left
more ▼

answered Mar 01 '11 at 03:36 PM

robert 4 gravatar image

robert 4
396 13 16 27

(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Jul 09 '12 at 06:21 AM

octaviomejiadiaz gravatar image

octaviomejiadiaz
0 2 5 7

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3689
x2088
x887
x788
x10

asked: Mar 01 '11 at 03:33 PM

Seen: 3068 times

Last Updated: Jul 09 '12 at 06:21 AM