Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
1
Question by mbreen · Oct 02, 2010 at 01:49 AM · slidersnapinterval

Slider snap to interval

Is there anyway to make set values for a slider that when slid it will snap to those values? For example say I have a slider with a value of 0 to 10. Can I have 10 values in-between so when you slide it, it snaps to 1 then 2 then 3, ect. Instead of being able to freely slide it to whatever value you want.

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

Answer by Loius · Oct 02, 2010 at 03:34 AM

You could cast the result of .Slider to an int, which would lock it to integers. Or you could go more fancy and write your own locking code -

int result = GUI.Slider(blah);

if ( result < 10 ) result = 0; else if ( result < 20 ) result = 10;

etc..

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 mbreen · Oct 02, 2010 at 05:43 PM 0
Share

Gotcha that makes sense thanks!

avatar image
4

Answer by $$anonymous$$ · Feb 14, 2018 at 12:37 AM

Here's a versatile piece of code to round to any interval. You can include this in a function that gets called when the slider's value is changed.

 float value = slider.value;
 float interval = 0.2f; //any interval you want to round to
 value = Mathf.Round(value / interval) * interval;
 slider.value = value;



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 vincent_oke · Feb 23 at 09:07 AM 0
Share

this is the correct answer, made a class and simply add this to your Slider UI:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class SliderValueSnap : MonoBehaviour
 {
     public float snapInterval = 5; //any interval you want to round to
     private Slider sliderUI;
 
     void Start()
     {
         sliderUI.onValueChanged.AddListener(delegate { ShowSliderValue(); });
         ShowSliderValue();
     }
 
     public void ShowSliderValue()
     {
         float value = sliderUI.value;
         value = Mathf.Round(value / snapInterval) * snapInterval;
         sliderUI.value = value;
     }
 }



avatar image
0

Answer by jordimeije · Sep 29, 2018 at 06:59 PM

Glitch???

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

GUI slider controls sunlight color? 3 Answers

Adjusting a GUI slider with the arrow keys? c# 1 Answer

Use gui slider to change text field and vice versa? 1 Answer

Overall Quality Horizontal Slider 1 Answer

GUI: Borders on a zero-width object 0 Answers


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