- Home /
Better way of making a 4.6UI slider to return even values only
Hi,
I'm using this script to make my slider return only even values but it's not smooth at all, do you have an idea on how to improve that behaviour ?
It's difficult to pass from one value to another
I'm using an inputfield to display the slider's value and you can see the value like flickering between the numbers not allowed and the even one i'm trying to go on.
Slider sliderComponent;
// Use this for initialization void Start () { sliderComponent = GetComponent<Slider> (); } // Update is called once per frame void Update () { if (sliderComponent.value % 2 != 0) sliderComponent.value--; if (sliderComponent.value < 0) sliderComponent.value = 0; }
EDIT: I solved the 'flickering' problem by only displaying even values in my input field, still a bit hard to pass from one value to another but it looks far more nicer now ^^
EDIT 2: In fact, on an uneven number i display the previous even number in my input field so i always display an even number, even when i move the cursor over an uneven ... problem solved.
@zeppike Lerp makes things not working anymore since when i'm on an uneven number it should skip it instantly and not take time to do it smoothly. But thanks anyway :)
Answer by KdRWaylander · May 19, 2015 at 09:06 AM
The script I use for hiding uneven values:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PairValuesSlider : MonoBehaviour {
Slider sliderComponent;
// Use this for initialization
void Start () {
sliderComponent = GetComponent<Slider> ();
}
// Update is called once per frame
void Update () {
if (sliderComponent.value % 2 != 0)
sliderComponent.value--;
if (sliderComponent.value < 0)
sliderComponent.value = 0;
}
}
You could store the last value in a float and check if the new value (when sliderComponent.value % 2 != 0) is greater than or smaller than the last value. If it is smaller, set the value to the closest even number below the current value. If it is bigger, set the value to the closest even number above the current value. That should make it pretty smooth.
Your answer
Follow this Question
Related Questions
Triple Slider control - UI slider value control 1 Answer
Getting the Value of a slider with 4.6 UI 2 Answers
How to Make a Vertical-Scroll Number Input Field, i.e. < > Value Amount 0 Answers
UI Slider Value Increases Far Too Slowly With Controller. 1 Answer
4.6 UI "Slider" jumps on handle press? 2 Answers