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 NeonTheCoder · Dec 01, 2018 at 01:46 AM · editorwindoweditorguilayouteditor scripting

Remove space between fields in BeginHorizontal

So I have an issue with EditorGUILayout.BeginHorizontal and fields within it.

No matter what I use the labelfields within a horizontal field will space themselves out across a horizontal area rather than snap right next to each other. You can resize the window and see the fields actively move to add/remove the space on their own to fill out the space equally. I've tried using ExpandWidth and labelWidth but nothing changes with their usage. alt text

This is my code:

  private GUIStyle AreaStyleNoMargin {
             get {
                 GUIStyle s = new GUIStyle(EditorStyles.textArea) {
                     margin = new RectOffset(0, 0, 0, 0),
                 };
                 return s;
             }
         }
         private GUIStyle AreaStyleNoPadding {
             get {
                 GUIStyle s = new GUIStyle(EditorStyles.textArea) {
                     padding = new RectOffset(0, 0, 0, 0)
                 };
                 return s;
             }
         }
         private GUIStyle AreaStyleNoMarginNoPadding {
             get {
                 GUIStyle s = new GUIStyle(EditorStyles.textArea) {
                     margin = new RectOffset(0, 0, 0, 0),
                     padding = new RectOffset(0, 0, 0, 0),
 
                 };
                 return s;
             }
         }
 
 //....
         private void DrawDatabaseEntries() {
             CheckInit();
 
             EditorGUILayout.BeginVertical();
             {
                 EditorGUILayout.BeginHorizontal();
                 {
                     Rect DBList = EditorGUILayout.BeginVertical(AreaStyleNoPadding, GUILayout.MaxWidth(((position.width / 4) * 3) + 30), GUILayout.MaxHeight((position.height / 4) * 3), GUILayout.MinHeight((position.height / 4) * 3));
                     {
                         dbEntriesScroll = GUILayout.BeginScrollView(dbEntriesScroll);
                         {
                             GUILayout.Box("", EditorStyles.toolbar, GUILayout.ExpandWidth(true));
                             GUILayout.Space((EditorGUIUtility.singleLineHeight * -1f) - 2.5f);
                             //Sorter Tabs
                             GUILayout.BeginHorizontal();
                             {
                                 if (GUILayout.Button("ID", TabStyleNoMargin)) {
                                     //DBSort = ManagerDBSort.LoadOrder;
                                     //SortEditorDB();
                                 }
                                 if (GUILayout.Button("Name", TabStyleNoMargin)) {
                                     //DBSort = ManagerDBSort.Name;
                                     //SortEditorDB();
                                 }
                             }
                             GUILayout.EndHorizontal();
 
                             if (selectedDatabase != null) {
                                 foreach (DatabaseEntry dbE in selectedDatabase.entries) {
                                     EditorGUILayout.BeginHorizontal(AreaStyleNoMarginNoPadding);
                                     EditorGUIUtility.labelWidth = 1;
                                     EditorGUILayout.LabelField(dbE.ID, AreaStyleNoMargin, GUILayout.MaxWidth(60), GUILayout.MinWidth(60), GUILayout.MinHeight(EditorGUIUtility.singleLineHeight + 2), GUILayout.ExpandWidth(false));
                                     EditorGUILayout.LabelField(dbE.Name, AreaStyleNoMargin, GUILayout.MaxWidth(60), GUILayout.MinWidth(60), GUILayout.MinHeight(EditorGUIUtility.singleLineHeight + 2), GUILayout.ExpandWidth(false));
                                     EditorGUIUtility.labelWidth = 0;
                                     EditorGUILayout.EndHorizontal();
                                 }
                             }
                                 
                             //Draw Lines
                             Handles.BeginGUI();
                             Handles.color = Color.gray;
                             for (int i = 0; i < 7; i++) {
                                 Handles.DrawLine(new Vector2(DBList.position.x + (100 * (i + 1)) - 2, DBList.position.y - 20), new Vector2(DBList.position.x + (100 * (i + 1)) - 2, DBList.position.y + DBList.size.y));
                             }
                             
                             Handles.EndGUI();
 
                         }
                         GUILayout.EndScrollView();
                     }
                     EditorGUILayout.EndVertical();
                 }
                 EditorGUILayout.EndHorizontal();
             }
             EditorGUILayout.EndVertical();
         }
weirdspace.png (11.0 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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Jul 24, 2020 at 11:15 AM

The layout system will distribute all left over space to all flexible spaces. If you want to collect all the left over space at one point, use GUILayout.FlexibleSpace(). So place this as the last element in your layout group. you may want to have a look at my IMGUI crash course. I have a mirror version on github since UnityAnswers has suddenly messed up the markup formatting.


ps: you shouldn't use a LabelField. A LabelField is meant to provide a label for some other control or for a simple text field. Have a look at the source code. If you just want to display data, just use a GUILayout.Label.


Though keep in mind that Unity does not have a "table" layout. Each layout group is independent from other groups. To me it seems you try to build a table. Two or more seperate horizontal layout groups won't align each other unless their content has the exact same size. It's better to provide a fix column size for each element. Keep in mind that you can use "GUILayout.Width(yourWidth)" as a GUILayoutOption to pretty much any layouted control.

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 KingsHere · Jul 24, 2020 at 08:29 AM

Updating the answer after trying out @Bunny83 's comment and answer in Unity Editor:

For me this helped :

                     GUILayout.FlexibleSpace();
                     EditorGUILayout.EndHorizontal();
 

Putting the GUILayout.FlexibleSpace(); before EditorGUILayout.EndHorizontal(); is the right way. All the spacing is managed automagically.

alt text


spacing.jpg (15.0 kB)
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
-1

Answer by Daniellsy · Jul 25, 2020 at 02:09 PM

private GUIStyle AreaStyleNoMargin { get { GUIStyle s = new GUIStyle(EditorStyles. textArea) { margin = new RectOffset(0, 0, 0, 0), }; return s; } }

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 Monkeydl_1929 · Mar 19, 2019 at 03:37 AM

@NeonTheCoder I found the answer to this question here:

https://forum.unity.com/threads/much-too-large-space-between-label-and-field-how-to-fix.312963/

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

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

Related Questions

Dynamic Editor Window Layout 0 Answers

Custom editor window stopped showing up 1 Answer

Small GUILayout button 1 Answer

Change script from EditorWindow 0 Answers

Populating object fields in a loop - editor scripting 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