Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
2
Question by DreadKyller · Dec 17, 2017 at 06:19 AM · guiguilayoutguilayout.beginhorizontal

Custom Editor GUILayout.Width isn't working when using variables, but works when not

There's an Edit at the bottom of post:

I'm working on a custom Editor for a script. I have begun a horizontal layout using: GUILayout.BeginHorizontal();

Now I want to space my elements out nicely, so I tried using GUILayout.Width() on the elements. The following below works fine (when I enter the values manually and not use variables):

 case SerializedPropertyType.Float:
     isFloat = true;
     goto case SerializedPropertyType.Integer;
 case SerializedPropertyType.Integer:
     GUILayout.BeginHorizontal ();
 
     GUILayout.Label (name, GUILayout.Width(200));
     if (isFloat) {
         brush_sp.floatValue = GUILayout.HorizontalSlider (brush_sp.floatValue, slide.min, slide.max, GUILayout.Width(130));
         brush_sp.floatValue = EditorGUILayout.FloatField (brush_sp.floatValue, GUILayout.Width(70));
     } else {
         brush_sp.intValue = Mathf.RoundToInt (GUILayout.HorizontalSlider (brush_sp.intValue, slide.min, slide.max));
         brush_sp.intValue = EditorGUILayout.IntField (brush_sp.intValue);
     }
     GUILayout.EndHorizontal();
     break;

The code above results in the following: alt text

Obviously though this doesn't change with the inspector width, so I made the following changes:

 public float width
 {
     get { return EditorGUILayout.GetControlRect (GUILayout.Height(0)).width;}
 }

And

 case SerializedPropertyType.Float:
     isFloat = true;
     goto case SerializedPropertyType.Integer;
 case SerializedPropertyType.Integer:
     float w = width;
     GUILayout.BeginHorizontal ();
 
     float label_width = Mathf.Floor(w * 0.5f);
     float slider_width = Mathf.Floor(label_width * 0.65f);
     float field_width = Mathf.Floor(label_width - slider_width);
     
     GUILayout.Label (name, GUILayout.Width(label_width));
     if (isFloat) {
         brush_sp.floatValue = GUILayout.HorizontalSlider (brush_sp.floatValue, slide.min, slide.max, GUILayout.Width(slider_width));
         brush_sp.floatValue = EditorGUILayout.FloatField (brush_sp.floatValue, GUILayout.Width(field_width));
     } else {
         // Not Implemented
     }
     GUILayout.EndHorizontal();
     break;

Only change was getting the width and using it for the sizes instead of hard-coding them, and it completely messes them up, I have even used labels to display the value of w and the 3 width variables and even when all the values are exactly identical it doesn't work and I get this instead: alt text

I have spent several hours trying to fix this problem and nothing changes the result, even just replaying 1 of the elements with a variable width instead of manually entering the number messes it up. Does anyone have an idea what's wrong?

Edit: I decided to stop using the supplied Layout classes, and wrote my own manager for simplifying the use of Rects with the standard GUI and EditorGUI classes. I am having no problems with this anymore. I still am however very confused how calling the Width function with a variable made it not work, while typing the value in manually did, I even did an equality check to ensure the numbers were actually equal. I may keep this open encase anyone knows a solution to the problem still, if not to help me at least to help anyone else who may run into this problem. Feel free to close this question if you are a moderator and feel it shouldn't stay open.

img-unity-issue2.png (5.4 kB)
img-unity-issue.png (7.4 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by redeemer · Oct 17, 2019 at 12:42 AM

A little late I know, but I've just run into this same problem, and maybe someone else finds this helpful. I can only guess why it's not working with variables, sorry, I supose they don´t want the component width to be driven by a parameter that can change it's value over time, so instead, they want us to use the correct options. Even if it's not that way, what I've done for not having to write my own implementation for managing the Rects (btw, congrats for that!!!), is to set the MinWidth for the elements and then use GUILayout.ExpandWidth(true), someting like :

GUILayout.BeginHorizontal(GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true));

You just have to use the same min width in all your elements if you want them to be equal. If not, use a number like 1 or 10 or 100 something like that to stablish the standard and asing the others the % difference. For exaple if you want other to be half that, just asign 0.5, 5 or 50 to its min width.

On the other hand, it's very important to have all your elements inside the correct "Vertical" and "Horizontal" spaces. I've seen inside your "Horizontals" you don´t use any "Vertical" for the columns, if you want them to match sizes, this isi a must.

For example, for creating a line with buttons all the same size that always match the width of the inspector you should use something like this (this will create two lines, the first one with 4 buttons and the second one with 3, but all of them in the same row will have the same width):

     public override void OnInspectorGUI() {
 
         EditorGUILayout.BeginVertical(GUILayout.ExpandWidth(true));
         EditorGUILayout.BeginHorizontal();
         
         EditorGUILayout.BeginVertical();
         if(GUILayout.Button("Button 1", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {
 
         }
         EditorGUILayout.EndVertical();
 
         EditorGUILayout.BeginVertical();
         if (GUILayout.Button("Button 2", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {
 
         }
         EditorGUILayout.EndVertical();
         EditorGUILayout.BeginVertical();
         if (GUILayout.Button("Button 3", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {
 
         }
         EditorGUILayout.EndVertical();
         EditorGUILayout.BeginVertical();
         if (GUILayout.Button("Button 4", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {
 
         }
         EditorGUILayout.EndVertical();
 
         EditorGUILayout.EndHorizontal();
         EditorGUILayout.EndVertical();
 
         EditorGUILayout.BeginVertical(GUILayout.ExpandWidth(true));
         EditorGUILayout.BeginHorizontal();
 
         EditorGUILayout.BeginVertical();
         if (GUILayout.Button("Button 5", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {
 
         }
         EditorGUILayout.EndVertical();
 
         EditorGUILayout.BeginVertical();
         if (GUILayout.Button("Button 6", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {
 
         }
         EditorGUILayout.EndVertical();
         EditorGUILayout.BeginVertical();
         if (GUILayout.Button("Button 7", GUILayout.MinWidth(100f), GUILayout.ExpandWidth(true))) {
 
         }
         EditorGUILayout.EndVertical();
 
         EditorGUILayout.EndHorizontal();
         EditorGUILayout.EndVertical();
 
     }

alt text

Hope this was helpful.

Regards.


example.png (14.7 kB)
Comment
Add comment · Show 1 · 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 sebastiankloch_baltoro · Oct 28, 2021 at 08:46 AM 0
Share

For me worked MinWidth and MaxWidth used together. Otherwise width would be always at MinWidth.

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

176 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 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

How to make Grid Layout Buttons in Custom Editor Window? 2 Answers

Help in changing the GUI looks! 0 Answers

Keep data on CustomEditor after moving from the GameObject with the editor to another GameObject. 0 Answers

GUI question, need some help 0 Answers

GUI Highscore? 0 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