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
1
Question by Charcolle · Dec 02, 2017 at 01:02 PM · editorwindowguistyleeditor extension

GUIStyle and GUILayout size

I have question about GUIStyle.

I often apply GUIStyle to GUILayout and some GUIStyle expand GUILayout even if I dont assign size.

     void OnGUI() {
         EditorGUILayout.BeginHorizontal();
         {
             left();
             right();
         }
         EditorGUILayout.EndHorizontal();
     }
 
     private void left() {
         EditorGUILayout.BeginVertical();
         {
             GUILayout.Box( "default box", GUI.skin.box );
             GUILayout.Box( "default box", GUI.skin.box );
         }
         EditorGUILayout.EndVertical();
     }
 
     private void right() {
         EditorGUILayout.BeginVertical();
         {
             GUILayout.Box( "RL Background", new GUIStyle( "RL Background" ) );
             GUILayout.Box( "RL Background", new GUIStyle( "RL Background" ) );
         }
         EditorGUILayout.EndVertical();
     }

alt text

Why does GUIStyle have a difference about size? In case of "RL Background", how can I control the size like "default box"?

guistyle.png (6.6 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
3
Best Answer

Answer by Bunny83 · Dec 02, 2017 at 01:31 PM

A GUIStyle has a setting called stretchHeight and stretchWidth which controls if the element should use as much space as available. The way the layout system works is that during the layout event Unity collects information about all elements. The elements which have a fixedWidth / Height are considered first. Their values are all added up and subtracted from the available space. The remaining space would usually be distributed between all expanding elements within the same layout group. However the distribution depends on the element sizes.


Instead of creating a new GUIStyle for every possibility one can add one or multiple GUILayoutOptions to almost all elements. There's a "params" array at the end of most control methods which allows that. The available GUILayoutOptions are:


  • ExpandHeight / ExpandWidth

  • Height / Width

  • MinHeight / MinWidth

  • MaxHeight / MaxWidth


Those options can be added to the actual call to the control method to overwrite certain the settings of the used GUIStyle.


Also note that there's the FlexibleSpace control. It will eat up any "left over space" which makes expandable elements as small as possible to fit all content.


You may want to have a look at my IMGUI CrashCourse. Unfortunately the latest changes on UnityAnswers renders the post like a wall of text. Paragraph spaces and bulletpoint indention has been removed which makes it look kinda messy.


Also note that when using the layout system in an EditorWindow or in a custom inspector the available space is usually the whole editor window to begin with. However if you use it in a Game the available space is not setup at all. The overall space grows as needed from the top left up to the screen dimensions. To actually stretch across the screen you would need to create an area that covers the entire screen (or the area you want) using BeginArea / EndArea

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 Bunny83 · Dec 02, 2017 at 02:45 PM 0
Share

Just as example, the default Button style does not stretch it's height (it has a fixedHeight) but it does stretch it's width.

avatar image Charcolle · Dec 02, 2017 at 02:47 PM 0
Share

Thank you for amazing answer!
I have never seen stretchHeight and stretchWidth.

 var style = new GUIStyle( "RL Background" );
 style.stretchHeight = false;
 style.stretchWidth = false;  

I can control size of GUILayout as desired:)

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

72 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

Related Questions

Unity editor extension - create drag and drop (similar to Buildbox) 1 Answer

Is there any way to make a timeline feature in editor scripting? 1 Answer

Is there a way to get all editable fields of a component in a script? 2 Answers

Editor GUI text becoming misaligned 0 Answers

Creating a custom editor window with a grid 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