Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by Wylie · Oct 20, 2010 at 09:39 PM · guiresolutionguitext

How to use the Screen.width and Screen.height to script GUI text

I can't seem to figure out how to use the Screen.width and Screen.height correctly.

I have the resolution at 4:3, and no matter what, when I build what I am working on, the text shows up in the middle of the field as opposed to the right side of the screen as I intended.

Can anyone help me out with how to script the Screen.width and Screen.height with text I want to display on the screen, like through GUI.label?

function OnGUI(){

\\These are the two lines that I want to reposition GUI.Label(Rect(335, 75, 100, 50), "Player 1: " +scriptStaticVarScoring.playerRed);

 GUI.Label(Rect(335, 125, 100, 50), 
                                  "Player 2: " +scriptStaticVarScoring.playerBlue);

}

Comment
Add comment · Show 2
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 ClandestineMan · Oct 20, 2010 at 10:54 PM 0
Share

Could you post your script or a portion of the script that you are using?

avatar image Wylie · Oct 21, 2010 at 01:51 PM 0
Share

Hey, Clandestine$$anonymous$$an! I edited the question with the script in question.

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Wolfram · Oct 21, 2010 at 02:50 PM

Are you talking about the placement of the label on the screen, or the placement/alignment of text within that label? For the latter, you need to adapt the settings of the associated GUIStyle (check the docs for that, I never used it so far). For the former it is simple once you understand what Rect(x,y,w,h) does. Rect() always refers to the upper left corner of an object, and the coordinates are always relative to the upper left screen corner. So to place the upper left corner of an object at screen coordinates (200,100), use

Rect(200,100,widthOfObject,heightOfObject);

(with appropriately defined valued for widthOfObject and heightOfObject).

Now if you want to place that same object relative to the upper RIGHT screen corner, for example, you can compute the correct pixel position of the upper left corner of your object with with:

Rect((Screen.width-1)-200-widthOfObject,100,widthOfObject,heightOfObject);

This is analogue for the y coordinate.

Comment
Add comment · Show 3 · 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 Wylie · Oct 21, 2010 at 05:59 PM 0
Share

I do want the Label to be relative to the right edge of the screen.

I utilized the second example you posted, and it works when I run the program through the Unity3D interface, but when I build and run it, the text remains at the center of the screen.

Any ideas?

avatar image Wylie · Oct 21, 2010 at 08:52 PM 0
Share

Never$$anonymous$$d! I got it to work, it wasn't a problem with the scripting, it was a bug in the editor. I just updated the project to unity 3.0 and it built great. Thanks for the help!

avatar image Tethys · Aug 08, 2012 at 08:50 PM 0
Share

Thanks for sharing this answer! This just helped me a bunch as well! Good stuff!

avatar image
0

Answer by JDonavan 1 · Oct 21, 2010 at 02:53 PM

If you want the text right justified in the rect you're going to need to pass in a style to the GUI.Label. See: http://unity3d.com/support/documentation/ScriptReference/GUIStyle.html

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Appear after set number of seconds 2 Answers

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 0 Answers

Is that a resolution-related issue? 1 Answer

Display GUI when kill count reaches 5 1 Answer

Screen Resolution and GUI cutoff issue. Android. 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