Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Grady · Jul 05, 2011 at 03:48 AM · guiscreenlabelmiddle

GUI in the middle of the screen

hey guys,

I am making a GUI Label appear in the middle of the screen, and this is the code that I have...:

 GUI.Label(Rect(Screen.width - Screen.width/2,Screen.height - Screen.height/2,250,300), "Hello World");

Now there are no errors in the console, but when I play my game in the editor (and I made the editor window popup out and as big as possible), the GUI Label isn't in the middle of the screen.... :/ It's to the left and a bit down..... :/

Would it be because it's in the editor, or is there error in my code????

Thanks

-Grady

Comment
Add comment · Show 1
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 Clydeicus · Jul 05, 2011 at 04:32 AM 0
Share

Is the top right corner of the label in the middle of the screen?

1 Reply

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

Answer by aldonaletto · Jul 05, 2011 at 04:36 AM

You should take the Rect width and height in account:

   var w = 250;
   var h = 300;
   private var rect = Rect((Screen.width-w)/2, (Screen.height-h)/2, w, h);
   GUI.Label(rect, "Hello World");

But if you want the Rect to be proportional to the Screen size (0=0%, 1=100%), you may use this:

   var w = 0.3; // proportional width (0..1)
   var h = 0.2; // proportional height (0..1)
   private var rect: Rect;
   rect.x = (Screen.width*(1-w))/2;
   rect.y = (Screen.height*(1-h))/2;
   rect.width = Screen.width*w;
   rect.height = Screen.height*h;
   GUI.Label(rect, "Hello World");

Despite the rect is centered, the text will still appear at the upper left rect corner. The problem here is the text alignment: it should be MiddleCenter to be centered in the rect passed to GUI.Label, but it's UpperLeft. In order to change this, you must add a GUIStyle variable to your script, set the desired characteristics by script or in the Inspector and pass it as the last parameter of the GUI function:

   ...
   var style: GUIStyle;
   style.alignment = TextAnchor.MiddleCenter;
   GUI.Label(rect, "Hello World", style);

Another alternative is to clone the style of other GUI itens. If you use the "button" style, for instance, the rect you've specified will appear with the text aligned to the center:

   GUI.Label(rect, "Hello World", "button");

This style is good to see exactly how the rect appear in the screen, but since it's the default button style, may confuse people (everybody will click it's a button).

Comment
Add comment · Show 7 · 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 Grady · Jul 05, 2011 at 07:15 AM 0
Share

but doesn't that mean that i would have to set the screen resolution on each startup if i was using a different computer?

avatar image aldonaletto · Jul 05, 2011 at 09:44 AM 0
Share

No, the Screen.width and Screen.height properties are always updated to the current resolution, and the code above keeps the rect center always at the screen center. On the other hand, if you want your rect to be proportional to the screen, you should use the 2nd version (answer edited to include it)

avatar image Grady · Jul 05, 2011 at 09:56 AM 0
Share

the text is still half off the side of the screen... should'nt there be a specific value that you can set to make it go specifically in the middle?

are the variable values supposed to be different?

avatar image aldonaletto · Jul 05, 2011 at 10:15 AM 0
Share

Ok, I got it: I'm setting the rect to the middle of the screen, but GUI.Label doesn't show the rect, it just shows the text. If the rect was visible, you would see it well centered in the screen. Let me check something - I'll be back soon.

avatar image Grady · Jul 05, 2011 at 10:50 AM 0
Share

the text in now closer to the centre of the screen, but it is still to the left a little, but using your example, i have sorted created the following:

GUI.Box(Rect(Screen.width /2 - 100,Screen.height /2 - 100,250,200), "Hello World");

it seems that dividing the screen height and width and then $$anonymous$$using 100 from that seems to move the box to the center of the screen on each different aspect ration and size of my screen, but i thank you of your efforts, and using your example, i have managed to solve my problem!!!!

The Hello World text in your script didn't move to stay at the same aspect ratio when the screen size was changed which is a bit strange, but never $$anonymous$$d...

Thanks :D

-Grady

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI button in middle of screen 2 Answers

Why do a GUI.label and GUI.textfield not align vertically? 1 Answer

Fix Resizing For GUI Label? 1 Answer

How to set text(timer) to a fixed place 1 Answer

How to clear a GUI Label 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