Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 patrick-noten · Nov 07, 2016 at 03:02 PM · unity 5editorfloatsliderdecimal

How to limit the amount of decimals with a slider in Unity Editor OnInspectorGUI?

So I have been fiddling around with the editor a lot and made a cool mostly horizontal script for level editting simple layer based levels. For this I have sliders that control the height of the layered walls which can vary between 2f and 3f and can be any value in between, except I would like to limit this to go in steps of 0.1f.

Currently I have:

 EditorGUI.Slider(new Rect(posLeft, pos.y, sliderWidth, rowHeight), rightScale, 2.0F, 3.0F, GUIContent.none);


I tried looking and ways to go around this by seperating the value shown and the slider but haven't found anything that limits the stepsize of the slider. I know the ways to convert float values by either Rounding the value or using ToString("0.#") but as the slider decides what value is shown I can't manipulate it later in the editor script.

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

Answer by Adam-Mechtley · Nov 07, 2016 at 09:43 PM

If you round your float value before passing it to the Slider() method, and round the value you get back from the Slider() method, that should do the trick. You can save yourself that calculation of the rounding you get back by wrapping your call to Slider() in EditorGUI.BeginChangeCheck()/EndChangeCheck(). For example:

 rightScale = Mathf.Round(rightScale * 10f) * 0.1f;
 EditorGUI.BeginChangeCheck();
 {
     EditorGUI.Slider(new Rect(posLeft, pos.y, sliderWidth, rowHeight), rightScale, 2f, 3f, GUIContent.none);
 }
 if (EditorGUI.EndChangeCheck())
 {
     rightScale = Mathf.Round(rightScale * 10f) * 0.1f;
     // apply rightScale back to your object or whatever
 }

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 patrick-noten · Nov 10, 2016 at 08:58 AM 0
Share

Thanks man that does the trick, I didn't know about BeginChangeCheck() and EndChangeCheck().

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity doesn't open 3 Answers

Coroutine works in editor, but not in build Windows application 0 Answers

Unity 5 C#, having a script show up in a path in the Editor 1 Answer

I dont want Decimals :P 1 Answer

Can't capture Multiple Screenshots in one Second 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