• 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 bliitzkriegx · Jul 25, 2013 at 05:34 PM · guiunitygui

GUI wont distribute evenly

I have a GUILayout of 4 buttons to pick the profile my user wants to play on. When it loads it grabs the stats from playerprefs and fills the non-empty profiles but the problem is it makes the grid of 2x2 uneven and look messy. Is there a way to force it to be even

T$$anonymous$$s is how it looks:

alt text

Thanks!

Edit*: Here is my code:

 if(isProfile){    
         GUILayout.BeginArea( new Rect(Screen.width / 2 - 250, Screen.height / 2 - 300, 500, 840) );
         GUILayout.BeginVertical();
         GUILayout.FlexibleSpace();
         GUILayout.BeginHorizontal();
         if(GUILayout.Button(p1String)){
                 PlayerPrefs.SetString("currentProfile", "1");
                 Application.LoadLevel("gameStage");
         }
         if(GUILayout.Button(p2String)){
                 PlayerPrefs.SetString("currentProfile", "2");
                 Application.LoadLevel("gameStage");
             }
         GUILayout.EndHorizontal();
         GUILayout.EndVertical();
         GUILayout.BeginVertical();
         GUILayout.BeginHorizontal();
         if(GUILayout.Button(p3String)){
                 PlayerPrefs.SetString("currentProfile", "3");
                 Application.LoadLevel("gameStage");
             }
         if(GUILayout.Button(p4String)){
                 PlayerPrefs.SetString("currentProfile", "4");
                 Application.LoadLevel("gameStage");
             }
         GUILayout.EndHorizontal();
         GUILayout.EndVertical();
         GUILayout.FlexibleSpace();
         GUILayout.EndArea();
         //back to menu
         if(GUI.Button(new Rect(20, 20, ButtonSize.width, ButtonSize.height), "Back")){
                 isProfile=false;
             }
     }





dsfd.png (184.3 kB)
Comment
Add comment · Show 4
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 DaveA · Jul 25, 2013 at 05:35 PM 0
Share
avatar image bliitzkriegx · Jul 25, 2013 at 05:57 PM 0
Share
avatar image gamerant · Jul 26, 2013 at 12:18 AM 0
Share
avatar image bliitzkriegx · Jul 26, 2013 at 12:40 PM 0
Share

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by perchik · Jul 26, 2013 at 07:09 PM

W$$anonymous$$le both of the above answers will solve your problem, they're not what you want. For a bunch of buttons laid out in a grid, the selection grid will work, but suppose you wanted to have the fourth box (of the same size) to be a different GUI element, the selection grid would not work.

Similarly, hardcoding the button button sizes and positions works fine, but opens up a whole different can of worms. If your GUI is designed in photoshop beforehand or you have a fixed display size and resolution, then a hard positioned GUI isn't terrible to build. On the other hand, if you want to be flexible, it takes a lot more work with setting the sizes and positions.

With all that said, here's how to do it with GUILayout:

 **Vector2 boxSize = GUI.skin.button.CalcSize(new GUIContent(p3String));**
         
         GUILayout.BeginArea(new Rect(Screen.width / 4, Screen.height /4, Screen.width/2, Screen.height/2));
         GUILayout.BeginVertical();
             GUILayout.BeginHorizontal();
                 **if (GUILayout.Button(p1String,GUILayout.MinWidth(boxSize.x)))**
                 {
                     PlayerPrefs.SetString("currentProfile", "1");
                     Application.LoadLevel("gameStage");
                 }
                 
                 **if (GUILayout.Button(p2String, GUILayout.ExpandHeight(true)))**
                 {
                     PlayerPrefs.SetString("currentProfile", "2");
                     Application.LoadLevel("gameStage");
                 }
                 
            GUILayout.EndHorizontal();
         GUILayout.EndVertical();
 
         GUILayout.BeginVertical();
         GUILayout.BeginHorizontal();
             if (GUILayout.Button(p3String))
             {
                 PlayerPrefs.SetString("currentProfile", "3");
                 Application.LoadLevel("gameStage");
             }
             if (GUILayout.Button(p4String))
             {
                 PlayerPrefs.SetString("currentProfile", "4");
                 Application.LoadLevel("gameStage");
             }
             GUILayout.EndHorizontal();
         GUILayout.EndVertical();
         GUILayout.FlexibleSpace();
         GUILayout.EndArea();

I starred the relevant lines. Basically you can use GUIStyle.CalcSize to get the amount of space that would be taken up by the guiContent of that style. (In t$$anonymous$$s case, since you are drawing buttons, I used the GUIStyle for the default button). Then you can use a GUILayout option to set the minimum width of the first box to the width of the bigger buttons. I also made the second profile box expand height to match.

To make t$$anonymous$$s more generic, you could loop over every button and find the max width and height, and set each button to use that width and height.

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
avatar image
0

Answer by Jamora · Jul 26, 2013 at 12:30 AM

You could use a SelectionGrid. It would handle the layouting itself. You could also override the precalculated button width and height by using GUILayoutOptions.

EDIT: You see a working example of SelectionGrid in the GUI Scripting Guide under Controls.

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
avatar image
0

Answer by gamerant · Jul 26, 2013 at 04:23 PM

I'm not sure w$$anonymous$$ch comment you are refering to mine or Jamora's :D If it's Jamora's then you'll have to do some research yourself as I have no idea about selection grid yet.

As for mine.I took a look at your code and in it everyt$$anonymous$$ng is fine until you pass information into the buttons.They simply deform.But if the second button contains information then everyt$$anonymous$$ng seems to even out.Not sure why though.

Also what I did with your code was simply change

 if(GUILayout.Button(p1String))
 {
   //What is inside here is unaltered.
 }

to

 if(GUI.Button(new Rect(100,100,100,100),p1String))
 {
   //your logic
 }

Still does the same t$$anonymous$$ng but you can control the size and position of the button.

I strongly suggest you take a look at Jamora's links as they are helpful :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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 avatar image avatar image

Related Questions

UnityGUI using Materials instead of Textures 0 Answers

Stretching GUI with rest of video? 0 Answers

How to touch up the UI in real time? 1 Answer

Creating Modular Unity GUI Components 1 Answer

Can I change the keycode that clicks the focused Unity GUI button? 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