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 moghes · Jan 21, 2013 at 06:15 PM · guibuttonguilabel

Positioning GUI.Button just next to GUI.Label for any text length

Hello everyOne! I need to display a text and a button next to it. I might send the GUI.Label text dynamically and don't know the accurate size. For now I have placed the label at 0 with a padding, and the button far away at Screen.width/2 , so that for a long word/words they don't override each other. Of course both have same height.. Is there a method that I can place the button just after the GUI.Label, and I seperate them with only padding. thanks in advance

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
1
Best Answer

Answer by BHViper · Jan 21, 2013 at 10:02 PM

You could try to get your labelText.Length, but I think CalcSize works out better in the end. If you happen to have a background box behind your label, CalcSize will keep the length accurate even if certain characters take more space than others. Below is a quick example:

 public string labelTextHere = "Edit in Inspector";
 private int leftPosition = Screen.width / 2;
 private int topPosition = Screen.height / 2;
 private int boxHeight = 20;
 private float newBoxWidth;
 private GUIStyle myStyle = new GUIStyle();
 Vector2 sizeOfLabel;
 
 void Start() {
     myStyle.alignment = TextAnchor.MiddleCenter;
     //any other styles
 }
 
 void OnGUI() {

     //Calculates the size of your label text with myStyle
     sizeOfLabel = myStyle.CalcSize(new GUIContent(labelTextHere));

     //sizeOfLabel.x will get the length. +6 can be adjusted for padding    
     newBoxWidth = sizeOfLabel.x + 6;                    
 
     //The background box behind the label, just for the example.
     GUI.Box(new Rect(leftPosition - newBoxWidth / 2, topPosition + 15, newBoxWidth, boxHeight), "");
     
     //The label text, this example is centered
     GUI.Label(new Rect(leftPosition - newBoxWidth / 2, topPosition + 15, newBoxWidth, boxHeight), labelTextHere, myStyle);
 
     //The button, drawn to the right of wherever the label ends.
     GUI.Button(new Rect(leftPosition + newBoxWidth / 2, topPosition + 15, 25, boxHeight), "");
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 moghes · Jan 22, 2013 at 09:36 AM 0
Share

thanks dude, your post seems to be helpful, i'll try it.

avatar image
2

Answer by gungnir · Feb 19, 2016 at 01:47 PM

I think these days this is the better answer: http://docs.unity3d.com/ScriptReference/EditorGUILayout.PrefixLabel.html

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

11 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

Related Questions

Full Gui-Based user interface memory leak 0 Answers

Guy Label on multi monitors 1 Answer

GUI textfield duplicate text 0 Answers

GUI.Button not responding when drawn in a Rect. 2 Answers

Text that follows players and scale according to camera zoom 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