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
1
Question by rd42 · May 21, 2010 at 02:59 PM · floatintegerhorizontal-slider

GUI.HorizontalSlider precision

Can HorizontalSlider be configured to only slide through whole numbers, 1, 2, 3, 4, 5... and not decimals, 0, 2.631579, 5.233158...

Thank you.

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

3 Replies

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

Answer by rd42 · May 21, 2010 at 03:14 PM

Here it is, I think. I had the var holding the slider value set as a float, when changed to int. All is well.

Any one know why some times when you paste code into this text box after pressing 101010 it doesn't format it right? See example below.

var velMagnitude: int = 0;

function OnGUI () { // Initial Velocity GUI.Label(Rect(3,300,200,50), "Initial Velocity"); velMagnitude = GUI.HorizontalSlider(Rect(3,320,200,20),velMagnitude,0,500); GUI.Label(Rect(220,320,50,20), velMagnitude.ToString()); }

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 Eric5h5 · Jun 04, 2010 at 04:21 PM 0
Share

You select your code, then press the code button, like I just did.

avatar image
2

Answer by geniuscd · Sep 18, 2012 at 12:45 PM

in c#

 public class sliderExample: MonoBehaviour{
 
 float repairOmeter = 0.0f;
 
   void OnGUI(){
     GUI.Label(new Rect(10,20,100,20),"Repair Meter Over Time");
     repairOmeter = GUI.HorizontalSlider(new Rect(25, 45, 100, 30), repairOmeter, 0.0F, 10.0F);
     repairOmeter = (int)repairOmeter;//only for Int
   }
 }

hope this helps :), I guess this is exactly what you need :P

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 Tigrou777 · Apr 23, 2016 at 10:51 AM

Instead of casting float to int, I would rather suggest to use Mathf.Round(). In most cases it will slip snap the slider to a position closer to mouse cursor. Eg: 3.9 will be snapped to 4 instead of 3.

 void OnGUI(){
    value = GUI.HorizontalSlider(new Rect(...), repairOmeter, 0.0f, 10.0f);
    value = Mathf.Round(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

2 People are following this question.

avatar image avatar image

Related Questions

int myX = (int)transform.position.x; 1 Answer

C# divide float by integer 2 Answers

Why aren't these numbers going down by 1. 1 Answer

Floating Point and ToString issue 2 Answers

How to i set the int to hitpoints / 10 + the current int. 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