• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by dorpeleg · Nov 26, 2011 at 05:05 PM · cameraguigameobjectvectorwindow

GUI Window on GameObject location?

How can i know the X,Y for the window that i want to PopUp so it will always show up on the same position of the GameObject?

I currently have this but it wont work:

         Vector3 t = unitToPopup.transform.position;
         GUI.Window(0, new Rect(this.camera.WorldToScreenPoint(t).x, this.camera.WorldToScreenPoint(t).y, 120, 50), CreateUnitWindow, "My Window");
Comment
aldonaletto

People who like this

1 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image

Answer by aldonaletto · Nov 26, 2011 at 05:51 PM

You could use this:

  Vector3 pos = Camera.main.WorldToScreenPoint(unitToPopup.transform.position);
  GUI.Window(0, new Rect(pos.x, pos.y, 120, 50), CreateUnitWindow, "My Window");
You only can use the camera variable when the object to which this script is assigned is a camera or have a camera component (added, not childed!).

EDITED:
The difference from your code is the use of Camera.main instead of camera, but this will not make any difference if this script is attached to the camera.
Doing a second reading, I suppose your problem is caused by the different Y orientations between GUI and screen points: GUI Y origin is at the screen top, while screen coordinates start at the bottom. Another problem: the window top-left will be aligned to the object's center, what may not give a good result - maybe you should add some offset to make it appear above the object. Reversing the Y coordinate and adding an offset could be done this way:

// define an offset value to keep the window top above the object
public float offset = 60; //offset in pixels
  ...
  Vector3 pos = Camera.main.WorldToScreenPoint(unitToPopup.transform.position);
  // make Y coordinate run from top, and add some offset to keep it above the object
  Rect wRect = new Rect(pos.x, Screen.height-(pos.y+offset), 120, 50);
  GUI.Window(0, wRect, CreateUnitWindow, "My Window");
NOTE: WorldToScreenPoint may return a valid screen coordinate even when the object is behind the camera; if you have this problem, check if renderer.isVisible is true before rendering the window (this actually tells if the object is inside the view frustum of any camera):

  if (unitToPopup.renderer.isVisible){
    Vector3 pos = Camera.main.WorldToScreenPoint(unitToPopup.transform.position);
    ...
    GUI.Window(0, wRect, CreateUnitWindow, "My Window");
  }

Comment
dorpeleg

People who like this

1 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dorpeleg · Nov 26, 2011 at 07:47 PM 0
Share

You wrote the exact code that i wrote.. my code does compile, but the window just wont pop in the right location.

avatar image aldonaletto · Nov 27, 2011 at 03:53 PM 0
Share

You should have explained your problem more clearly than "it wont work": I thought your script didn't work at all, and supposed it was the ubiquitous camera variable problem. From your comment, I figured out that another very often mistake (so often that I made the same mistake in my answer) may be the culprit. Take a look at my answer: I edited it to address this problem and other possible ones.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

picture in picture 3d. 0 Answers

GUI controlling other game objects 0 Answers

"Press [e] to open" on stationary object when onTrigger 0 Answers

2 cameras: 1 for GUI and 1 for GameObjects 3 Answers

Render GameObjects over GUI.Layer 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges