Slider Component Reference

Slider is a component for the production of UI components such as volume adjustment.

slider-inspector

Click the Add Component button at the bottom of the Inspector panel and select UI/Slider to add the Slider component to the node.

To use Slider, please refer to the Slider API documentation and the Slider scene of the test-cases-3d project.

Slider Properties

PropertiesFunction Explanation
HandleThe button part of the Slider that allows to adjust value by sliding the button
DirectionThe direction of the slider, including Horizontal and Vertical
ProgressCurrent progress value, the value range is 0 ~ 1
SlideEventsSlider component event callback function

Slider Event

slider-event

For event structure you can refer to the Button documentation.

The Slider event callback has two parameters, the first one is the Slider itself and the second is the customEventData.

Detailed Explanation

The Slider is usually used to adjust the value of the UI (for example, volume adjustment), and its main component is a slider button, which is used for user interaction. You can adjust the value of the Slider through this part.

Usually a Slider node tree as shown below:

slider-hierarchy

Add a callback by script code

Method one

The event callback added by this method is the same as the event callback added by the editor, all added by code. First you need to construct a EventHandler object, and then set the corresponding target, component, handler and customEventData parameters.

  1. import { _decorator, Component, Event, Node, SliderComponent, EventHandler } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass("example")
  4. export class example extends Component {
  5. onLoad(){
  6. const sliderEventHandler = new EventHandler();
  7. // This Node is the node to which your event processing code component belongs
  8. sliderEventHandler.target = this.node;
  9. // This is the script class name
  10. sliderEventHandler.component = 'example';
  11. sliderEventHandler.handler = 'callback';
  12. sliderEventHandler.customEventData = 'foobar';
  13. const slider = this.node.getComponent(SliderComponent);
  14. slider.slideEvents.push(sliderEventHandler);
  15. }
  16. callback(event: Event, customEventData: string){
  17. // The event here is a Touch Event object, and you can get the send node of the event by event.target
  18. // The customEventData parameter here is equal to the "foobar" you set before
  19. }
  20. }

Method two

By slider.node.on('slide', ...) way to add.

  1. // Suppose we add event handling callbacks to the onLoad method of a component and perform event handling in the callback function:
  2. import { _decorator, Component, SliderComponent } from 'cc';
  3. const { ccclass, property } = _decorator;
  4. @ccclass("example")
  5. export class example extends Component {
  6. @property(SliderComponent)
  7. slider: SliderComponent | null = null;
  8. onLoad(){
  9. this.slider.node.on('slider', this.callback, this);
  10. }
  11. callback(slider: SliderComponent){
  12. // The parameter of the callback is the Slider component. Note that events registered this way cannot pass customEventData
  13. }
  14. }