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
2
Question by Alieos · Apr 01, 2014 at 02:46 PM · c#guimenuslideroptions

Horizontal Slider with Labels

Hi,

I was just wondering if there is a simple way to label a GUILayout horizontal slider dynamically as such (shown in the image below):

alt text

I want the '100' to appear at the right end of the slider and I want it to be dynamic so that whenever I resize the whole menu, the '100' remains at the desired position.

I'm coding in C#.


Thank you in advance for your help.

screenshot.png (16.5 kB)
Comment
Add comment · Show 1
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 Scribe · Apr 05, 2014 at 02:45 PM 0
Share

If your question has been answered I'd appreciate if you marked it as such, or please explain if the code posted isn't working for you and I can try to help!

Scribe

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Scribe · Apr 02, 2014 at 06:23 PM

You could try changing the text alignment of the second label to be UpperRight rather than Left, something like:

 var progress : float = 50;
 var min : float = 0;
 var max : float = 100;
 
 function OnGUI(){
     GUILayout.BeginArea(Rect(10, 10, 200, 100));
     progress = GUILayout.HorizontalSlider(progress, min, max);
         GUILayout.BeginHorizontal();
             var defaultAlignment = GUI.skin.label.alignment;
             GUILayout.Label(min.ToString());
             GUI.skin.label.alignment = TextAnchor.UpperRight;
             GUILayout.Label(max.ToString());
             GUI.skin.label.alignment = defaultAlignment;
         GUILayout.EndHorizontal();
     GUILayout.EndArea();
 }

Scribe

Comment
Add comment · Show 3 · 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 Alieos · Apr 06, 2014 at 10:28 AM 0
Share

It works with some tweaking. But there's a small space above the "100". I'm using the UpperRight alignment.

alt text

screenshot 2.png (12.2 kB)
avatar image Scribe · Apr 06, 2014 at 11:48 AM 0
Share

Somewhere after defaultAlignment is defined can you print it and see what it is using by default Debug.Log(defaultAlignment)

It looks to me like your '0' label might be using '$$anonymous$$iddleLeft' or 'LowerLeft'

Depending on how often you change the skin elsewhere, you might find it a good idea to set up an if statement something like:

 if(GUI.skin.label.alignment == TextAnchor.UpperLeft){
     defaultAlignment = GUI.skin.label.alignment;
     newAlignment = TextAnchor.UpperRight;
 }else if...
avatar image Alieos · Apr 07, 2014 at 04:14 AM 0
Share

Ok. I'll just work on my own from here. Thanks for your help.

avatar image
0

Answer by Estecka · May 08, 2018 at 05:22 PM

Using EditorGUI instead of GUILayout, Scribe's solution didn't work for me as such. I made it through tweaking it that way :

alt text

 Rect position = EditorGUILayout.GetControlRect (false, 2*EditorGUIUtility.singleLineHeight); // Get two lines for the control
 position.height *= 0.5f;
 tgt.FitOrFill = EditorGUI.Slider (position,"Fit or Fill", tgt.FitOrFill, 0, 1);

 // Set the rect for the sub-labels
 position.y     += position.height;
 position.x     += EditorGUIUtility.labelWidth;
 position.width -= EditorGUIUtility.labelWidth + 54; //54 seems to be the width of the slider's float field

 //sub-labels
 GUIStyle style = GUI.skin.label;
 style.alignment = TextAnchor.UpperLeft;  EditorGUI.LabelField (position, "Fit",  style);
 style.alignment = TextAnchor.UpperRight; EditorGUI.LabelField (position, "Fill", style);



slider.jpg (1.5 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
0

Answer by Voxel-Busters · Dec 17, 2018 at 03:59 PM

Just cleaning up a bit more.

 SerializedProperty bitRateFactor = property.FindPropertyRelative("m_bitRateFactor");
 
 position = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
 
 // Draw label
 position = EditorGUI.PrefixLabel(position, new GUIContent("Bitrate Factor"));
 EditorGUI.indentLevel--;
 
 // Draw slider
 bitRateFactor.floatValue = EditorGUI.Slider(position, bitRateFactor.floatValue, 0f, 1f);
 float labelWidth = position.width;
 
 // Move to next line
 position.y          += EditorGUIUtility.singleLineHeight;
 
 // Subtract the text field width thats drawn with slider
 position.width      -= EditorGUIUtility.fieldWidth;
 
 GUIStyle style = GUI.skin.label;
 TextAnchor defaultAlignment = GUI.skin.label.alignment;
 style.alignment = TextAnchor.UpperLeft; EditorGUI.LabelField(position, "Low Bitrate", style);
 style.alignment = TextAnchor.UpperRight; EditorGUI.LabelField(position, "High Bitrate", style);
 GUI.skin.label.alignment = defaultAlignment;


And here is how it looks - alt text


sliderwithlabels.png (9.9 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

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

23 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

Related Questions

When I pause my game and enable canvas and then resume my keyboard starts controlling the menu... 1 Answer

Menu Building - Cannot implicity convert type float to int 2 Answers

How can i open in game manu (GUI) with button? 2 Answers

Distribute terrain in zones 3 Answers

how to control SMOOTHNESS via GUI slider?! It's not working...pls help! 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