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 SwastikBhattacharyya · Mar 29, 2020 at 07:19 AM · editor-scriptingeditorwindowlabelalignmenteditorscript

EditorGUILayout.LabelField not centered properly

I have created an editor window script to create an editor window. I have a Rect variable headerRect with the following properties:

         headerRect.x = 0f;
         headerRect.y = 0f;
         headerRect.width = Screen.width;
         headerRect.height = 50f;
         GUI.DrawTexture(headerRect, headerRectTexture);

Here is the function that creates a label in the headerRect section.

     private void DrawHeaderSection() // Draws the Header Section
     {
         GUILayout.BeginArea(headerRect);
         EditorGUILayout.LabelField("CHARACTER EDITOR", guiSkin.GetStyle("Header"));
         GUILayout.EndArea();
     }

The GUISkin has a style called "Header" and it mentions that the alignment is MiddleCenter but still the alignment is not proper. Rather, it seems that the text is starting from the middle of the width. The following image will demonstrate the problem. alt text

How do I properly align the label to the centre?

labelalignmentproblem.png (168.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
Best Answer

Answer by SwastikBhattacharyya · Mar 29, 2020 at 07:24 AM

The Solution


Change the Fixed Width field in your GUISkin file
Enter the value of your screen width to that field ie. if the width of the headerRect is 600 then write 600 in the Fixed Width field in the inspector.



Change the Fixed Width of your style through Code
Change the DrawHeaderSection() function to this:

     private void DrawHeaderSection() // Draws the Header Section
     {
         GUILayout.BeginArea(headerRect);
         guiSkin.GetStyle("Header").fixedWidth = 600;
         EditorGUILayout.LabelField("CHARACTER EDITOR", guiSkin.GetStyle("Header"));
         GUILayout.EndArea();
     }

Add the following line before drawing the label
guiSkin.GetStyle("Header").fixedWidth = 600;
NOTE: You might be thinking why did I put 600 instead of Screen.width. It's because Screen.width was returning 750 instead of 600 which is the wrong value.

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

137 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

Related Questions

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

How to get Event.KeyboardEvent from non-focused EditorWindow? 0 Answers

Populating object fields in a loop - editor scripting 1 Answer

Saving a variable from an editor script into something like a gamemanager script? 0 Answers

Dynamic content in custom EditorWindow 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