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 Borzi · Oct 03, 2013 at 12:22 PM · guilayoutscreen.heightbeginareanew rect

Get Area to Spread Equally with Screen.Height or Screen.Width

Hey guys, I am trying to build an Area that works with the screen width (so it adjusts to resolution), but the Area expands to the right instead of staying in the center and spreading out equally. Any idea of how to do this correctly?

 GUILayout.BeginArea(new Rect((Screen.width/2), (Screen.height/2), Screen.height * (3f/5f), Screen.width * (0.5f/5f)), GUIContent.none, "box");

Thank you in advance!

Comment
Add comment · Show 3
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 ShadoX · Oct 03, 2013 at 12:59 PM 0
Share

That's because first two values of new Rect represent the top left coordinates from where the Rectangle starts and the other two the dimensions of it.

http://docs.unity3d.com/Documentation/ScriptReference/Rect-ctor.html http://docs.unity3d.com/Documentation/ScriptReference/Rect.html

You'll also have to adjust the first two to change the position accordingly to the the size.

avatar image Borzi · Oct 03, 2013 at 06:42 PM 0
Share

Okay I'll look into it, although this didn't really help me that much into getting it perfectly into the center. But thanks!

avatar image robertbu · Oct 03, 2013 at 07:21 PM 0
Share

If you have something of some width (w) and some height (h), you want to center using GUI, you build the Rect as:

 new Rect(Screen.width/2f - w/2, Screen.height/2 - h/2, w, h)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tw1st3d · Oct 03, 2013 at 07:29 PM

Here, try this. Instead of using 3f/5f, try using a direct fraction.

 GUILayout.BeginArea(new Rect((Screen.width/2), (Screen.height/2), Screen.width * 0.6f, Screen.height * 0.1), GUIContent.none, "box");
Comment
Add comment · 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 Borzi · Oct 04, 2013 at 09:34 AM 0
Share

Nope has to be a divided thing or its not gonna work I believe, at least I get compiler errors while the other method has always worked.

avatar image tw1st3d · Oct 04, 2013 at 11:30 PM 0
Share

Oh whoops, I missed an f after 0.1. Try doing Screen.height * 0.1f

avatar image
0

Answer by Jamora · Oct 03, 2013 at 07:50 PM

If you want the Rect for your Area to be perfectly centered at all times, do

 aRect.center = new Vector2(Screen.width/2,Screen.height/2);
 GUI.Box(aRect,"CenteredBox");

Now you can edit aRect.width and aRect.height while keeping the Rect centered.

Comment
Add comment · Show 4 · 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 Borzi · Oct 04, 2013 at 09:35 AM 0
Share

Interesting...but will this work just like GUI Layout (that buttons will adjust to it, etc.)?

avatar image Jamora · Oct 04, 2013 at 09:40 AM 0
Share

It will work for GUILayout, too. Just remember to set the center before drawing the area and you're set.

         aRect.center = new Vector2(Screen.width/2,Screen.height/2);
         GUILayout.BeginArea(aRect,"","Box");
         GUILayout.EndArea();
avatar image Borzi · Oct 04, 2013 at 11:38 AM 0
Share

Okay so I did this, but I get compiler errors. First, even with the variable aRect defined, the console spits out "aRect" does not exist in the current context. Also, the GUILayout.BeginArea doesnt really seem to harmonize with aRect as well. But thanks for your answer :D If you want I can post a bigger code sample maybe Im doing it wrong.

avatar image Jamora · Oct 04, 2013 at 08:57 PM 0
Share

Please do post your code. There must be something wrong because the code works for me.

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Only using related functions, yet still being told that I can't use non GUI functions in OnGUI (Don't call OnGUI) 1 Answer

Make area adapt to screen size 1 Answer

Drawing several BeginArea inside a BeginScrollview (GUILayout) produces an unexpected behaviour 1 Answer

GUILayout.BeginArea expand to screen height? 0 Answers

How to solve GUI Layout problem 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