Unity allows us to add lower and upper bounds to sliders in a very simple way. Inside the Slider (Script) component, there are two variables named Min Value and Max Value. If we take the slider from the previous recipe and change the values of these variables, we'll see that we can drag the handle of the slider to the end and the value has an upper bound, as we can see in the following screenshot:
In this recipe, we will write a script to block the handle of the slider as well in order to make the player perceive a real upper or lower bound.
using UnityEngine.UI;
statement at the beginning of the script so that we can handle UI elements within the script. We can also add the following line before the class: [RequireComponent(typeof(Slider))]
(without the semicolon at the end). By doing this, the script requires a Slider (Script) component attached to the same game object of it in order for it to work. In fact, if it is not present, Unity will add it for us.private Slider slider; public float lowerBound; public float upperBound;
Awake()
function, we have to link the Slider
to our variable and then carry out our first check. To improve performance, future checks will be done only if the value of Slider
changes. We can perform our check by calling the checkBounds()
function, which we will write in the next step. Thus, we can write the Awake()
function as follows:void Awake () { slider = GetComponent<Slider> (); checkBounds (); }
check
function, we have to ensure that the value of Slider
is between the lower bound and the upper bound. If not, we have to change its value to the closer bound, like this:public void checkBounds () { if(slider.value>= upperBound){ slider.value = upperBound; } else { if(slider.value<= lowerBound){ slider.value = lowerBound; } } }
checkBounds()
function from our script is called, and it will check whether the new value of the slider is inside the bounds. If we have taken the slider from the previous recipe, we should see this:Since Unity doesn't allow us to block the handle of the slider directly, we need a script that controls it within the bounds. In fact, every time the OnValueChange
event occurs, the value of the slider is passed to our script. This checks whether the slider value is between the bounds. If it isn't, the script changes the value of the slider to the closest bound limit. For example, if the player tries to move the slider further than the bound, our script will make the slider come back to that bound. Finally, our script stores the bound limits in a couple of public variables so that we can change them during the gameplay with other scripts. This is useful because the player will perceive this as a block of the slider. Therefore, we can change the bounds to give more power to the player over time.
The following sections will give us some interesting suggestions on how to improve our Slider Shower.
Sometimes in games, we not only want to block the slider by creating bounds, but also to make the player feel the bounds. One way of doing this is by changing the color of the Slider Shower. To achieve this, we need to modify the script slightly. First, we should add the reference to the Slider Shower inside our script. Thus, we can add a new public variable into our script:
public Text uiText;
This will store the Text (Script) component attached to Slider Shower. Furthermore, we need to change the checkBounds()
function in the following way:
public void checkBounds () { if(slider.value>= upperBound){ slider.value = upperBound; uiText.color = Color.red; } else { if(slider.value<= lowerBound){ slider.value = lowerBound; uiText.color = Color.red; } } uiText.color = Color.white; }
By doing this, if a bound is reached, we change the color of Slider Shower to red. Otherwise, we can assign the color white to it. Finally, we need to drag and drop Slider Shower into the uiText variable of our script in the Inspector.
Sometimes, it's better to express the parameters in a different way, especially if designers have to tweak them. In fact, another way might be more understandable and comfortable. For example, instead of expressing a result in decimal points, we can display it more simply as a percentage. Here, you are going to learn how to express bounds as a percentage within the Inspector; this is easy to understand for designers. Therefore, let's start changing our script a little.
First of all, we need to change our variables. Consider these lines:
public float lowerBound; public float upperBound;
Replace them with the following lines:
[Range(0,100)] public int lowerBound; [Range(0,100)] public int upperBound;
In this way, we have changed the type of our variable in int
and added a Range
attribute in order to limit the possible values (between 0
and 100
) that can be set in the Inspector.
Now, since the slider value is a float between 0
and 1
, we also need to convert the percentage value into a decimal one in the checkBounds()
function. In fact, if we divide the value by 100f
, we obtain the decimal value. Therefore, we can rewrite the entire function in the following way:
public void checkBounds () { if(slider.value>= upperBound/100f){ slider.value = upperBound/100f; } else { if(slider.value<= lowerBound/100f){ slider.value = lowerBound/100f; } } }
As in the original function, we need to check whether the slider is exceeding the bounds, and if so, restore it to the closest bound.
If we want to limit the possible values that can be set in the Inspector, we have to add a Range
attribute to our variables. This prevents designers or us from setting a wrong number.
If we have expressed the value as a percentage, we can add the [Range(0,100)]
line before our variable in this way:
[Range(0,100)] public int lowerBound; [Range(0,100)] public int upperBound;
In fact, they are int
, and the percentage can be a number between 0
and 100
.
Otherwise, if we still express the bounds as floats in the Inspector, the Range
attribute is [Range(0,1)]
. Therefore, we can write the following:
[Range(0,1)] public float lowerBound; [Range(0,1)] public float upperBound;
In this case, the value of our variable can be a float
number between 0
and 1
.
3.146.221.149