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 DreadTalon · Mar 09, 2013 at 08:45 PM · bugguilayoutboxoverlapmargin

GUILayout margin bug?

GUILayout seems to only draw margins correctly at some window sizes.

Steps to reproduce:

  • Create a GUI area, it can be any proportions but it must scale with screen width.

  • Inside the area begin a horizontal layout with 3 (arbitrary) GUILayout boxes inside.

  • Apply a skin which sets the box style's margins for left and right to be 1 pixel (also arbitrary it seems).

  • Resize the editor.

As the screen size changes the margin between the boxes will increase and decrease, ranging from what appears to be 3 pixels to a 1 pixel overlap. It even seems to happen if you set the margin to zero, sometimes there is a visible gap and sometimes the borders overlap each other.

This seems like a bug in Unity and it does persist in real builds.

 public GUISkin skin;
   
 void OnGUI() {
     GUI.skin = skin;
     GUILayout.BeginArea(new Rect(10, 10, Screen.width / 2.0f, 400));
     GUILayout.BeginHorizontal();
     GUILayout.Box("A");
     GUILayout.Box("Column2");
     GUILayout.Box("Column3");
     GUILayout.EndHorizontal();
     GUILayout.EndArea();
 }

alt text

alt text

These pictures show the same layout at two slightly different editor window sizes, resulting in neither of them showing the correct margins.

Is this a Unity bug or a mistake somewhere on my part?

untitled.png (3.8 kB)
untitled1.png (2.5 kB)
Comment
Add comment
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

2 Replies

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

Answer by DreadTalon · Sep 05, 2013 at 08:15 PM

What appears to be happening is that the layout manager takes the width of the container area, divides it into even portions for each child element and tries to fit the correct margin between the two. Sometimes the number of pixels in the container turn out to be "odd" ie it isn't possible to have the same pixel margin between each element AND have all the elements be the same size. In this case the layout manager seems to prioritize having the elements the same size and simply disobeys the margin to make it fit.

I think the way I got around this issue was to just set a hardcoded value for the container that is known to fit correctly. It might not be resolution independent. If you needed a resolution independent solution it might be possible to use a RoundToNearestMultiple function so that you get the closest scale to what you want that doesn't have an odd number of pixels that cause the margins to fail.

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 jorashwil · Mar 09, 2013 at 09:13 PM

You have 2 options to my knowledge, either 1. use a sprite or 2. define a size for each box. The overlap is normal or residual is normal. It's like if you make a health bar and set it as: GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth, myStyle) you'll still be left with a dark line when it hits 0. This is because of the division of curHealth and maxHealth. So 0/100, returns 0, but the box doesn't disappear because they're is still a value of 0 out of 100.

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 DreadTalon · Mar 09, 2013 at 10:43 PM 0
Share

I'm not sure how it can be considered to be working as intended. If you set the margin to zero, sometimes there is a multiple pixel gap between the boxes. Isn't the correct behaviour to always have the margins obeyed and have the boxes sized appropriately?

avatar image jorashwil · Mar 10, 2013 at 05:56 PM 0
Share

Your dividing 1 big box not setting 3 boxes, try making 3 boxes ins$$anonymous$$d.

avatar image Bosak · Sep 05, 2013 at 02:50 PM 0
Share

I found the same bug and it still persists It is some Unity problem and it can't be fixed I think

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

12 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

Related Questions

Problem with editor layout on dual monitor setup 1 Answer

How do you change the margin, padding, and border size of GUILayout buttons? 1 Answer

GUILayout Positioning for SelectionGrids and Buttons 2 Answers

avoid overlap problem when randomly instantiating 0 Answers

Encase GUILayout area 3 Answers


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