SoFunction
Updated on 2025-03-06

Detailed explanation of the use of Slider sliding condition group of Unity UGUI

1. What is the Slider component?

Slider (slider bar) is a commonly used UI component in Unity UGUI, which is used to realize sliding selection function in the user interface.

By dragging the slider, the user can select an intra-value value range.

2. How the Slider component works

The Slider component consists of two parts: the sliding area and the slider. The sliding area is used to display the background of the slider, while the slider indicates the current numerical position.

The user can change the value by dragging the slider.

3. Common properties of Slider components

  • Min Value (minimum value): The sliding bar value is smallest **.
  • Max Value is the largest (largest) value: The maximum value of the slider is large.
  • Value (current value): The current value of the slider.
  • Whole Numbers (only allow integer values ​​to be selected only:)number.
  • Direction: The direction of the slide bar, which can be horizontal or vertical.
  • Handle Slide Area (slide) area motion slide: The slider can slide within the sliding area.
  • Handle Slide Range:The moving range of the slider sliding in the moving sliding area.

4. Common parts of Slider group

  • SetValueWithoutNotify(float value): Sets the current value of the slider, but does not trigger the OnValueChanged event.
  • OnValueChanged(UnityAction<float> action): Event triggered when the value of the slider changes.

5. Sample code

Example 1: Basic use

using UnityEngine;
using ;
public class SliderExample : MonoBehaviour
{
    public Slider slider;
    private void Start()
    {
        (OnSliderValueChanged);
    }
    private void OnSliderValueChanged(float value)
    {
        ("Slider value changed: " + value);
    }
}

Operation steps:

  • Create an empty object and add the Slider component to that object 2.
    . Add the SliderExample script to the object.
  • In the Inspector panel, associate the slider field of the SliderExample script to the Slider component.
  • Run the game, drag the slider, and observe the console output.

Notes:

  • By listening to the Slider's onValueChanged event, you can execute custom logic when the value of the slider changes.

Example 2: Limit the range of values

using UnityEngine;
using ;
public class SliderExample : MonoBehaviour
   {
    public Slider slider;
    private void Start()
    {
         = 0;
         = 100;
         = true;
    }
}

Operation steps:

  • Create an empty object and add the Slider component to the object.
  • Add the SliderExample script to add the object.
  • In the Inspector panel, associate the slider field of the SliderExample script to the Slider component, Game.4.
    Observe the numerical range of the slider and whether only integer values ​​are allowed to be selected.

Notes:

  • By setting the minValue and maxValue properties, you can limit the range of values ​​of the slider.
  • Only integer values ​​can be selected by setting the wholeNumbers property to true.

Example 3: Horizontal Slider Bar

using UnityEngine;
using ;
public class SliderExample : MonoBehaviour
{
    public Slider slider;
    private void Start()
    {
         = ;
    }
}

Steps:

  • Create an empty object and add the Slider component to the object.
  • Add the SliderExample script to the object.
  • In the Inspector panel, associate the slider field of the SliderExample script to the Slider component.
  • Run the game and observe the direction of the slider.

Notes:

  • By setting the direction property to LeftToRight, the slider can be set to the horizontal direction.

Example 4: Vertical Slider Bar

using UnityEngine;
using ;
public class SliderExample : MonoBehaviour
{
    public Slider slider;
    private void Start()
    {
         = ;
    }
}

Operation steps:

  • Create an empty object and add the Slider component to the object.
  • Add the SliderExample script to the object.
  • In the Inspector panel, associate the slider field of the SliderExample script to the Slider component.
  • Run the game and observe the direction of the slider.

Notes:

  • By setting the direction property to BottomToTop, you can set the slider to the vertical direction.

Example 5: Set the slider sliding range

using UnityEngine;
using ;
public class SliderExample : MonoBehaviour
{
    public Slider slider   ;
    private void Start()
    {
        <>().sizeDelta = new Vector2(20, 20);
    }
}

Operation steps:

  • Create an empty object and add the Slider component to the object.
  • Add the SliderExample script to the object.
  • In the Inspector panel, associate the slider field of the SliderExample script to the Slider component.
  • Run the game and observe the slider size of the slider.

Notes:

  • The sizeDelta property of the slider's RectTransform can be adjusted.

References

Unity UI Tutorial - Slider

The above is the detailed explanation of the use of the Slider sliding condition group of Unity UGUI. For more information about the Unity UGUI Slider sliding bar, please follow my other related articles!