• 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
Question by LukaKotar · Jan 29, 2013 at 09:46 AM · c#guisliderinteger

OnGUI sliders only accept float? C#

Hi.

I'd like to make an integer ('int') based GUI slider, similar to EditorGUI.IntSlider, running inside the game. I've tried GUI.HorizontalSlider, but it only allows for 'float'. How could I do that?

Thanks.

Comment
Lovrenc
whydoidoit

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Jan 29, 2013 at 10:05 AM

Just round it to "int" with Mathf.RoundToInt. EditorGUI.IntSlider does exactly the same thing ;)

 int val = 0;
 
 val = Mathf.RoundToInt(GUILayout.HorizontalSlider(val, 0, 10));
 // or
 val = Mathf.RoundToInt(GUI.HorizontalSlider(yourRect, val, 0, 10));
Comment
LukaKotar
whydoidoit
Flashmangagames

People who like this

3 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 LukaKotar · Jan 29, 2013 at 10:08 AM 0
Share

Works, thanks!

avatar image

Answer by ginryu · Jul 12, 2020 at 05:45 AM

yourValue = (int)GUI.HorizontalSlider(Rect yourRect, yourvalue, 2,25); you can cast your slider output to int instead of using the Mathf class

Comment

People who like this

0 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 Bunny83 · Jul 12, 2020 at 11:47 AM 0
Share

Just casting to an int has several issues. First of all you get a 0.5 bias. So the bottom and top value has strange behaviour. You can only reach your max value when you actually hit the very last position as any lower value would be rounded down (if the max limit is positive). So in your specific example all values between 2 and 3 will round down to 2. Values between 3 and 4 round to 3 [...] and values between 24 and 25 will round to 24.


Another issue is negative numbers as casting to int will not consistently "floor" the value but always rounds towards zero. So a .24.999 will be rounded down to 24 while a -24.999 will round to -24, not -25. So if your slider range goes across zero (from a negative value to a positive value) you get a strange anomaly at zero since all values between -1 and 1 will round to zero.


Using the Round function takes care of that as it does proper midpoint rounding

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Display actual number of bullets 1 Answer

Set Slider value but don't trigger On Value Changed 4 Answers

Horizontal slider won't work 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 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